I have a few Pandas DataFrames sharing the same value scale, but having different columns and indices. When invoking df.plot()
, I get separate plot images. what I really want is to have them all in the same plot as subplots, but I'm unfortunately failing to come up with a solution to how and would highly appreciate some help.
我有一些Pandas DataFrames共享相同的值规模,但具有不同的列和索引。当调用df.plot()时,我得到单独的绘图图像。我真正想要的是将它们全部放在与次要情节相同的情节中,但遗憾的是我未能提出如何并且非常欣赏一些帮助的解决方案。
6 个解决方案
#1
130
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):
您可以使用matplotlib手动创建子图,然后使用ax关键字在特定子图上绘制数据框。例如,对于4个子图(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
.
这里的轴是一个包含不同子图轴的数组,您只需通过索引轴即可访问它们。如果你想要一个共享的x轴,那么你可以为plt.subplots提供sharex = True。
#2
24
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:
你可以看到e.gs.在证明joris答案的文档中。同样从文档中,您还可以在pandas plot函数中设置subplots = True和layout =(,):
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.
您还可以使用fig.add_subplot()获取子图网格参数,如221,222,223,224等,如此处的帖子所述。在这个ipython笔记本中可以看到关于pandas数据框的情节的好例子,包括子图。
#3
11
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:
您可以使用熟悉的Matplotlib样式调用图形和子图,但您只需使用plt.gca()指定当前轴。一个例子:
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...
等等...
#4
1
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()
#5
#6
0
Building on @joris response above, if you have already established a reference to the subplot, you can use the reference as well. For example,
在上面的@joris响应的基础上,如果您已经建立了对子图的引用,您也可以使用该引用。例如,
ax1 = plt.subplot2grid((50,100), (0, 0), colspan=20, rowspan=10)
...
df.plot.barh(ax=ax1, stacked=True)
#1
130
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):
您可以使用matplotlib手动创建子图,然后使用ax关键字在特定子图上绘制数据框。例如,对于4个子图(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
.
这里的轴是一个包含不同子图轴的数组,您只需通过索引轴即可访问它们。如果你想要一个共享的x轴,那么你可以为plt.subplots提供sharex = True。
#2
24
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:
你可以看到e.gs.在证明joris答案的文档中。同样从文档中,您还可以在pandas plot函数中设置subplots = True和layout =(,):
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.
您还可以使用fig.add_subplot()获取子图网格参数,如221,222,223,224等,如此处的帖子所述。在这个ipython笔记本中可以看到关于pandas数据框的情节的好例子,包括子图。
#3
11
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:
您可以使用熟悉的Matplotlib样式调用图形和子图,但您只需使用plt.gca()指定当前轴。一个例子:
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...
等等...
#4
1
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()
#5
1
You may not need to use Pandas at all. Here's a matplotlib plot of cat frequencies:
您可能根本不需要使用熊猫。这是猫频率的matplotlib图:
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()
#6
0
Building on @joris response above, if you have already established a reference to the subplot, you can use the reference as well. For example,
在上面的@joris响应的基础上,如果您已经建立了对子图的引用,您也可以使用该引用。例如,
ax1 = plt.subplot2grid((50,100), (0, 0), colspan=20, rowspan=10)
...
df.plot.barh(ax=ax1, stacked=True)