How can I display individual Pandas DataFrames as subtitles?

I have several Pandas DataFrames that use the same scale of values, but have different columns and indexes. When df.plot() called, I get separate graphics. I really want all of them to be in the same plot as the subheadings, but, unfortunately, I didn’t come up with a decision on how much some help is appreciated.

+87
python matplotlib pandas
Mar 18 '14 at 15:18
source share
7 answers

You can manually create subheadings using matplotlib, and then plot the data on a specific subheading using the ax keyword. For example, for 4 subtitles (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 that contains different axes of the subtask, and you can access it simply by indexing axes .
If you want to use the common x axis, then you can provide sharex=True to plt.subplots .

+186
Mar 18 '14 at 15:45
source share

You can see, for example, in the documentation demonstrating the joris answer. Also from the documentation you can also set subplots=True and layout=(,) in the pandas plot function:

 df.plot(subplots=True, layout=(1,2)) 

You can also use fig.add_subplot() , which accepts subtask grid parameters such as 221, 222, 223, 224, etc., as described in the post here . Good examples of graphs on a pandas data frame, including subheadings, can be seen in this ipython laptop .

+45
Jun 04 '15 at 15:26
source share

You can use the familiar Matplotlib style that calls figure and subplot , but you just need to specify the current axis using plt.gca() . 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...

+14
Jul 12 '15 at 3:29
source share

You can use this:

 fig = plt.figure() ax = fig.add_subplot(221) plt.plot(x,y) ax = fig.add_subplot(222) plt.plot(x,z) ... plt.show() 
+3
Nov 16 '17 at 12:19
source share

You may not need to use pandas at all. Here is the plot of matplotlib feline frequencies:

enter image description here

 x = np.linspace(0, 2*np.pi, 400) y = np.sin(x**2) f, axes = plt.subplots(2, 1) for c, i in enumerate(axes): axes[c].plot(x, y) axes[c].set_title('cats') plt.tight_layout() 
+2
Mar 05 '18 at 23:13
source share

Based on @joris answer above, if you have already set a link to a subtitle, you can also use a link. For example,

 ax1 = plt.subplot2grid((50,100), (0, 0), colspan=20, rowspan=10) ... df.plot.barh(ax=ax1, stacked=True) 
+1
Dec 09 '16 at 15:32
source share

You can build multiple subplots of several pandas data frames using matplotlib, with a simple trick of listing all the data frames. Then use the for loop to build the subplots.

Work code:

 import matplotlib.pyplot as plt import pandas as pd import numpy as np # dataframe sample data df1 = pd.DataFrame(np.random.rand(10,2)*100, columns=['A', 'B']) df2 = pd.DataFrame(np.random.rand(10,2)*100, columns=['A', 'B']) df3 = pd.DataFrame(np.random.rand(10,2)*100, columns=['A', 'B']) df4 = pd.DataFrame(np.random.rand(10,2)*100, columns=['A', 'B']) df5 = pd.DataFrame(np.random.rand(10,2)*100, columns=['A', 'B']) df6 = pd.DataFrame(np.random.rand(10,2)*100, columns=['A', 'B']) #define number of rows and columns for subplots nrow=3 ncol=2 # make a list of all dataframes df_list = [df1 ,df2, df3, df4, df5, df6] fig, axes = plt.subplots(nrow, ncol) # plot counter count=0 for r in range(nrow): for c in range(ncol): df_list[count].plot(ax=axes[r,c]) count=+1 

enter image description here

Using this code, you can build subplots in any configuration. You just need to determine the number of nrow rows and the number of ncol columns. You also need to make a list of the df_list data df_list that you want to build.

0
Jun 23 '19 at 13:26
source share



All Articles