Broken axis example: uneven subplot size

UPDATE 2018 There is a github project https://github.com/bendichter/brokenaxes which probably is more convenient to use. My own solution looks like (using gridspec, assuming that the units of the two y-axis should be equal): “”” Broken axis example, where the y-axis will have a portion cut out. “”” import matplotlib.pylab as plt import matplotlib.gridspec as gridspec … Read more

How to create Pandas groupby plot with subplots

Here’s an automated layout with lots of groups (of random fake data) and playing around with grouped.get_group(key) will show you how to do more elegant plots. import pandas as pd from numpy.random import randint import matplotlib.pyplot as plt df = pd.DataFrame(randint(0,10,(200,6)),columns=list(‘abcdef’)) grouped = df.groupby(‘a’) rowlength = grouped.ngroups/2 # fix up if odd number of groups … Read more

Axes from plt.subplots() is a “numpy.ndarray” object and has no attribute “plot”

If you debug your program by simply printing ax, you’ll quickly find out that ax is a two-dimensional array: one dimension for the rows, one for the columns. Thus, you need two indices to index ax to retrieve the actual AxesSubplot instance, like: ax[1,1].plot(…) If you want to iterate through the subplots in the way … Read more

How to set xticks in subplots

There are two ways: Use the axes methods of the subplot object (e.g. ax.set_xticks and ax.set_xticklabels) or Use plt.sca to set the current axes for the pyplot state machine (i.e. the plt interface). As an example (this also illustrates using setp to change the properties of all of the subplots): import matplotlib.pyplot as plt fig, … Read more

How to add a title to each subplot

ax.title.set_text(‘My Plot Title’) seems to work too. fig = plt.figure() ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(222) ax3 = fig.add_subplot(223) ax4 = fig.add_subplot(224) ax1.title.set_text(‘First Plot’) ax2.title.set_text(‘Second Plot’) ax3.title.set_text(‘Third Plot’) ax4.title.set_text(‘Fourth Plot’) plt.show()