
Matplotlib is a widely-used Python Data Visualization library that provides a numerous selection of 2D and 3D plots that are very useful for data analysis and machine learning tasks.
The syntax for creating a complex plot may seem intimidating but it offers a great deal of flexibility. You can pretty much touch on any component on a plot and customize it.
In this post, I will show you how to create a basic plot and customize it with various touches. We will work on the scripting layer which is the matplotlib.pyplot interface. You are most likely to use this interface in your analysis.
import matplotlib.pyplot as plt
%matplotlib inline #render plots in notebook
Everything starts with creating a Figure which is the main Artist object that holds everything together.
plt.figure(figsize=(10,6))
We have created a Figure of size (10,6). Let’s plot a basic bar plot on this figure.
plt.figure(figsize=(10,6))
plt.bar(x=[3,4,2,1], height=[3,5,1,6])

The ticks on x-axis are too many. It will look better if only ticks for the bars are shown. Also, I want to extend the range on the y-axis so that the highest bar does not reach the top. We can also assign labels on x-ticks which I think looks nicer than plain numbers.
plt.figure(figsize=(10,6))
plt.bar(x=[3,4,2,1], height=[3,5,1,6])
plt.xticks(ticks=[1,2,3,4],
labels=['Rome','Madrid','Istanbul','Houston'])
plt.yticks(ticks=np.arange(10))

The labels look better with a higher punto. Let’s increase it and also rotate the labels using the rotation parameter. It would also be nice to add a title.
plt.figure(figsize=(10,6))
plt.title("Use Your Own Title", fontsize=15)
plt.bar(x=[3,4,2,1], height=[3,5,1,6])
plt.xticks(ticks=[1,2,3,4], labels=['Rome','Madrid','Istanbul','Houston'], rotation="45", fontsize=12)
plt.yticks(ticks=np.arange(10), fontsize=12)

We can add a frame to the Figure object with the facecolor parameter. The xlabel and ylabel functions can be used to add axis labels. The width of bars can also be adjusted using the width parameter of the bar function.
plt.figure(figsize=(10,6), facecolor="lightgreen")
plt.title("Use Your Own Title", fontsize=15)
plt.bar(x=[3,4,2,1], height=[3,5,1,6], width=0.5)
plt.xticks(ticks=[1,2,3,4], labels=['Rome','Madrid','Istanbul','Houston'], rotation="45", fontsize=12)
plt.yticks(ticks=np.arange(10), fontsize=12)
plt.xlabel("Cities", fontsize=14)
plt.ylabel("Measurement", fontsize=14)

Annotations and texts can be used on plots to make them more informative or deliver a message. Matplotlib allows to add them as well.
We will use an Axes object on the Figure to add text and annotations. In order to locate these add-ons on the Axes, we need to use the coordinate. Thus, it makes sense to also add grid lines which can be achieved by plt.grid() function.
Let’s first add the grid lines and a text box.
ax = plt.figure(figsize=(10,6), facecolor="lightgreen").add_subplot(111)
plt.title("Use Your Own Title", fontsize=15)
plt.bar(x=[3,4,2,1], height=[3,5,1,6], width=0.5)
plt.xticks(ticks=[1,2,3,4], labels=['Rome','Madrid','Istanbul','Houston'], rotation="45", fontsize=12)
plt.yticks(ticks=np.arange(10), fontsize=12)
plt.xlabel("Cities", fontsize=14)
plt.ylabel("Measurement", fontsize=14)
plt.grid(which='both')
ax.text(3.5, 8, 'Daily Average', style='italic', fontsize=12,
bbox={'facecolor': 'grey', 'alpha': 0.5, 'pad': 5})

The first two parameters of ax.text() function are x and y coordinates, respectively. After we specify the text and style, we add a box around it using the bbox parameter.
Let’s also add an annotation. I don’t want to repeat the same part of code so I will just write the part for annotation. You can add it to the code that produced the previous plot.
ax.annotate('highest', xy=(1, 6), xytext=(1.5, 7), fontsize=13, arrowprops=dict(facecolor='black', shrink=0.05))

We have added an arrow pointing at the top of the highest bar. The xy parameter contains the coordinates for the arrow and the xytext parameter specifies the location of the text. Arrowprops, as the name suggests, is used to style the arrow.
Once you are happy with your plot, you can save it using plt.savefig() function.
The purpose of this post was to show you the great flexibility of Matplotlib. The plots we have produced may not be useful at all but they deliver the message. The syntax may look unnecessarily long but that is the price to pay for flexibility.
It is worth mentioning that this is only a part of what you can create with Matplotlib. For instance, the structure of subplots is a whole different topic. Once you are comfortable with Matplotlib, there is no limit on what you can create.
Thanks for reading. Please let me know if you have any feedback.