How can I plot separate Pandas DataFrames as subplots?
Solution 1:
You can manually create the subplots with matplotlib, and then plot the dataframes on a specific subplot using the ax
keyword. For example for 4 subplots (2x2):
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=2)
df1.plot(ax=axes[0,0])
df2.plot(ax=axes[0,1])
...
Here axes
is an array which holds the different subplot axes, and you can access one just by indexing axes
.
If you want a shared x-axis, then you can provide sharex=True
to plt.subplots
.
Solution 2:
You can see e.gs. in the documentation demonstrating joris answer. Also from the documentation, you could also set subplots=True
and layout=(,)
within the pandas plot
function:
df.plot(subplots=True, layout=(1,2))
You could also use fig.add_subplot()
which takes subplot grid parameters such as 221, 222, 223, 224, etc. as described in the post here. Nice examples of plot on pandas data frame, including subplots, can be seen in this ipython notebook.
Solution 3:
You can use the familiar Matplotlib style calling a figure
and subplot
, but you simply need to specify the current axis using plt.gca()
. An example:
plt.figure(1)
plt.subplot(2,2,1)
df.A.plot() #no need to specify for first axis
plt.subplot(2,2,2)
df.B.plot(ax=plt.gca())
plt.subplot(2,2,3)
df.C.plot(ax=plt.gca())
etc...