pandas Matplotlib pyplot 并排放置两个图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52195838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib pyplot putting two plots side by side
提问by JAbrams
Hi I'm trying to put two plots side by side with plt and have tried the recommendations here: Trying to position subplots next to each other
嗨,我正在尝试将两个图与 plt 并排放置,并尝试了这里的建议:尝试将子图放置在彼此相邻的位置
however I am plotting using the default capabilities from pandas by doing
但是我正在使用Pandas的默认功能进行绘图
myDataFrame.plot(kind='scatter' x='xcol', y='ycol')
so i can't do plt.subplot(2, 1, 2)
as in myDataFrame.subplot(2,1,2)
(obviously)
所以我不能做plt.subplot(2, 1, 2)
,如myDataFrame.subplot(2,1,2)
(显然)
and doing
和做
plt.subplot(1, 2, 1)
myDataFrame.plot(kind='scatter' x='xcol', y='ycol')
plt.subplot(1, 2, 2)
myDataFrame.plot(kind='scatter' x='xcol', y='ycol')
just adds two plots before the plot i want
只是在我想要的情节之前添加两个情节
any ideas how i can still use myDataframe.plot(kind='scatter')
and put two of them side by side
任何想法我仍然可以使用myDataframe.plot(kind='scatter')
并将其中两个并排放置
回答by Sheldore
In case you are still looking for it, here is a minimal working answer for you (excluding imports)
如果您仍在寻找它,这里有一个最小的工作答案(不包括进口)
fig, ax = plt.subplots(ncols=2, figsize=(10,4))
df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
[6.4, 3.2, 1], [5.9, 3.0, 2], [7.1, 4.5, 2]],
columns=['X', 'Y', 'value'])
df.plot.scatter(x='X', y='Y', c='red', ax=ax[0])
df.plot.scatter(x='X', y='Y', c='red', ax=ax[1])
Output
输出
回答by JoshuaF
It looks like DataFrame.plot
takes an ax
argument for a pyplot axis https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html.
Try:
它看起来像一个 pyplot 轴DataFrame.plot
的ax
参数https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html。尝试:
ax = plt.subplot(1, 2, 1)
myDataFrame.plot(kind='scatter' x='xcol', y='ycol', ax=ax)