Advanced plots in Matplotlib — Part 2 | by Parul Pandey | Towards Data Science

This article comes as a second part in the Advanced Plots in Matplotlib series. In Part 1, we learnt how to use plots like Span selector, Broken Horizontal Bar plot and the table function in our visualisation work. We also learnt how to watermark images and add a twist to the plots in Matplotlib by using the XKCD theme. In part 2, we shall go over some of the other interesting capabilities of Matplotlib in action and use it to jazz up our existing plots.
towardsdatascience.com.
Event plots come in handy when one wants to plot identical parallel lines at pre-defined positions. Such plots are commonly used in neuroscience to display the spiking activity of the neurons over time, where it is frequently referred to as a spike raster or simply a raster plot. However, there are other uses for such kind of plots too. Here is an excerpt from the event plot documentation :
It is useful in any situation where you wish to show the timing or position of multiple sets of discrete events, such as the arrival times of people to a business on each day of the month or the date of hurricanes each year of the last century.
Let’s look at an eventplot showing sequences of events with various line properties. The plot is shown in both horizontal and vertical orientations. The plot has been adapted from official Matplotlib's Eventplot documentation.
Did you know that it is possible to create a simple timeline using Matplotlib alone? Well yes. This is because technically, timelines are simply collections of dates and text which can be obtained with simple variations in a stem plot. You can read more about it here.
Here is a timeline showing the Android version history using the code provided in the official documentation.
Have you ever wanted to further drill down into a pie chart? Maybe you wanted to expand one of its slices and ‘explode’ it into a bar chart? Matplotlib makes it possible through a ‘Bar of Pie’ functionality. It uses a ConnectionPatch that connects two points (possibly in different axes).
The idea is simple. Create a pie chart and its corresponding bar chart as subplots and then use a connection patch to draw lines between the two subplots.
Here is an example from the official documentation.
While creating plots in matplotlib, a lot of times, we tend to stick with the default style. However, Matplotlib offers a bunch of great style options which make even the mundane visualizations really stand out. To list all of the styles, enter the following line of code.
Here I’ll showcase some of the popular ones. In case you are interested, you can find the complete list here. Let’s create a basic line plot and then apply some of the different styles. When no style has been specified, matplotlib will use the default style:
import matplotlib.pyplot as pltplt.plot([1, 3, 9, 5, 2, 1, 1], marker='o')plt.plot([4, 5, 5, 7, 9, 8, 6], marker='v')plt.plot([2, 3, 4, 3, 4, 5, 3], marker='s')plt.show()
To add a style of your choice, insert the following line in your code:
plt.style.use('stylename') #Replace 'stylename' with the desired style
Let’s see some of the commonly used styles:
Classic is Matplotlib’s older style.
import matplotlib.pyplot as pltplt.style.use(“classic”)plt.plot([1, 3, 9, 5, 2, 1, 1], marker=’o’)plt.plot([4, 5, 5, 7, 9, 8, 6], marker=’v’)plt.plot([2, 3, 4, 3, 4, 5, 3], marker=’s’)plt.show()
This style emulates the aesthetics of ggplot (a popular plotting package for R)
import matplotlib.pyplot as pltplt.style.use(“ggplot”)plt.plot([1, 3, 9, 5, 2, 1, 1], marker=’o’)plt.plot([4, 5, 5, 7, 9, 8, 6], marker=’v’)plt.plot([2, 3, 4, 3, 4, 5, 3], marker=’s’)plt.show()
This shows an example of the “fivethirtyeight” styling, which tries to replicate the styles from fivethirtyeight.com
import matplotlib.pyplot as pltplt.style.use(“fivethirtyeight”)plt.plot([1, 3, 9, 5, 2, 1, 1], marker=’o’)plt.plot([4, 5, 5, 7, 9, 8, 6], marker=’v’)plt.plot([2, 3, 4, 3, 4, 5, 3], marker=’s’)plt.show()
This example demonstrates the style used in the Bayesian Methods for Hackers online book.
import matplotlib.pyplot as pltplt.style.use("bmh")plt.plot([1, 3, 9, 5, 2, 1, 1], marker='o')plt.plot([4, 5, 5, 7, 9, 8, 6], marker='v')plt.plot([2, 3, 4, 3, 4, 5, 3], marker='s')plt.show()
Recently, I stumbled upon a package named mplcyberpunk, which is a Python package on top of matplotlib to create 'cyberpunk' style plots with just three additional lines of code. How cool is that 🤘?
To use the cyberpunk style; you will need to install and import the library first.
!pip install mplcyberpunk  import matplotlib.pyplot as pltimport mplcyberpunkplt.style.use("cyberpunk")plt.plot([1, 3, 9, 5, 2, 1, 1], marker='o')plt.plot([4, 5, 5, 7, 9, 8, 6], marker='v')plt.plot([2, 3, 4, 3, 4, 5, 3], marker='s')# Add glow effects-Optionalmplcyberpunk.add_glow_effects()plt.show()
Lastly, let’s look at a useful Third-party package in Matplotlib. As the name suggests, third-party packages build and extend the existing Matplotlib capabilities. An important point to keep in mind is that these packages are not included in Matplotlib by default and have to be installed separately.
A lot of times, we struggle to adjust the text positions in a graph. This happens when there are multiple labels, and these labels start overlapping. adjustText is a pretty useful library for such situations as it automates the placement of labels.
As already stated, you’ll need to install the library first, which can be done in either of the three ways:
pip install adjustText # pip installorconda install -c conda-forge adjusttext # condaor# For the latest version from GitHub:pip install https://github.com/Phlya/adjustText/archive/master.zip
Let’s look at a basic example from adjustText’s documentation itself to highlight the power of adjustText. The figures on the left show the overlapping labels while the figure on the right shows the auto-adjusted labels obtained after calling the adjust_text function.
This brings us to the end of our two-part series on advanced Matplotlib plots. In this series, we saw how the Matplotlib visualization library could be leveraged to produce some unique charts. This goes a long way in creating a compelling storyline. Whereas Event Plots are super useful in plotting discrete events, timelines help to portray specific events in history. Again, a bar of pie conveys much more information than a traditional pie chart. Styles like ‘Cyberpunk’ provide an element of classy style that can make some of the visualizations stand out. Hopefully, you’ll be able to utilize some of the information shared in these articles in your visualizations and share your cool projects with the world.
[1]Originally published at parulpandey.com
[2]This is an Affiliate link