# Amending Commits, Matplotlib, and More Python

- Link: https://cagrimmett.com/2016/04/06/matplotlib-amending-commits/
- Published: 2016-04-06T04:00:00-04:00
- Author: Chuck Grimmett

I’ve been on vacation and spend the last two days catching up and not doing a lot
of learning, so I’ve been lazy in putting up TIL posts. That is over. (I did, however,
push some updates to my [Apple Photos Analysis](https://github.com/cagrimmett/apple_photos_analysis)
project.) Here is a small collection of things I learned in the last week.

---

### Amending commits

Say you forgot to add a file to your last commit or you made a typo in your commit
message. You can amend it!

Make the necessary changes, then do this:

    ```language-shell
    git commit --amend -m "Commit message here"
    ```

If you’ve already pushed it to an external repository, you’ll need to force the 
push since the external repo will look like it is ahead. If [branch protection](https://help.github.com/articles/about-protected-branches/)
is turned on, you’ll need to make a new commit. Make sure you aren’t overwriting
anything important!

    ```language-shell
    git push origin master --force
    ```

[Here are the docs.](https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things)

---

### Adding data labels to the top of bar charts in Matplotlib

[Matplotlib](http://matplotlib.com) is a great plotting library for Python.

    ```python
    def autolabel(rects):     # attach some text labels     for rect in rects:         height = rect.get_height()         plt.text(rect.get_x() + rect.get_width()/2., 5+height,                 '%d' % int(height),                 ha='center', va='bottom') rect = plt.bar(xs, counted_hours, color=color)  # To use: autolabel(rect)
    ```

---

### Saving images in matplotlib

    ```python
    plt.savefig('directory/filename.png')
    ```

---

### Counting items that match a regex pattern

    ```python
    def hour_finder(regex,lines): 	time_counter = 0 	for l in lines: 		if re.match(regex, l): 			time_counter = time_counter + 1 	return time_counter 	 # To use hour_finder('^8:[0-9]{2,}:[0-9]{2,}sPM',time_csv)
    ```

---

### Splitting!

Splitting by a space `' '` and choose the item after the split (`[1]` because counting
starts at 0)

    ```python
    list = [i.split(' ')[1] for i in time_csv] 
    ```