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

Rotate tick labels in subplot

You can do it in multiple ways: Here is one solution making use of tick_params: ax.tick_params(labelrotation=45) Here is another solution making use of set_xticklabels: ax.set_xticklabels(labels, rotation=45) Here is a third solution making use of set_rotation: for tick in ax.get_xticklabels(): tick.set_rotation(45)

Subplot for seaborn boxplot

We create the figure with the subplots: f, axes = plt.subplots(1, 2) Where axes is an array with each subplot. Then we tell each plot in which subplot we want them with the argument ax. sns.boxplot( y=”b”, x= “a”, data=df, orient=”v” , ax=axes[0]) sns.boxplot( y=”c”, x= “a”, data=df, orient=”v” , ax=axes[1]) And the result is:

Adding subplots to a subplot

You can nest your GridSpec using SubplotSpec. The outer grid will be a 2 x 2 and the inner grids will be 2 x 1. The following code should give you the basic idea. import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec fig = plt.figure(figsize=(10, 8)) outer = gridspec.GridSpec(2, 2, wspace=0.2, hspace=0.2) for i in … Read more

Can I create AxesSubplot objects, then add them to a Figure instance?

Typically, you just pass the axes instance to a function. For example: import matplotlib.pyplot as plt import numpy as np def main(): x = np.linspace(0, 6 * np.pi, 100) fig1, (ax1, ax2) = plt.subplots(nrows=2) plot(x, np.sin(x), ax1) plot(x, np.random.random(100), ax2) fig2 = plt.figure() plot(x, np.cos(x)) plt.show() def plot(x, y, ax=None): if ax is None: ax … Read more