Plot all pandas dataframe columns separately

Pandas subplots=True will arange the axes in a single column. import numpy as np import matplotlib.pyplot as plt import pandas as pd df = pd.DataFrame(np.random.rand(7,20)) df.plot(subplots=True) plt.tight_layout() plt.show() Here, tight_layout isn’t applied, because the figure is too small to arange the axes nicely. One can use a bigger figure (figsize=(…)) though. In order to have … Read more

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

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:

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