在同一个 IPython Notebook 单元格中制作多个图表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16392921/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:28:36  来源:igfitidea点击:

Make more than one chart in same IPython Notebook cell

pythonpandasipythonipython-notebook

提问by WebOrCode

I have started my IPython Notebook with

我已经启动了我的 IPython Notebook

ipython notebook --pylab inline

This is my code in one cell

这是我在一个单元格中的代码

df['korisnika'].plot()
df['osiguranika'].plot()

This is working fine, it will draw two lines, but on the same chart.

这工作正常,它将绘制两条线,但在同一个图表上。

I would like to draw each line on a separate chart. And it would be great if the charts would be next to each other, not one after the other.

我想在单独的图表上绘制每条线。如果图表彼此相邻,而不是一个接一个,那就太好了。

I know that I can put the second line in the next cell, and then I would get two charts. But I would like the charts close to each other, because they represent the same logical unit.

我知道我可以将第二行放在下一个单元格中,然后我会得到两个图表。但我希望图表彼此靠近,因为它们代表相同的逻辑单元。

采纳答案by Rutger Kassies

Make the multiple axes first and pass them to the Pandas plot function, like:

首先制作多个轴并将它们传递给 Pandas 绘图函数,例如:

fig, axs = plt.subplots(1,2)

df['korisnika'].plot(ax=axs[0])
df['osiguranika'].plot(ax=axs[1])

It still gives you 1 figure, but with two different plots next to each other.

它仍然给你 1 个数字,但有两个不同的图彼此相邻。

回答by Tooblippe

You can also call the show() function after each plot. e.g

您还可以在每次绘图后调用 show() 函数。例如

   plt.plot(a)
   plt.show()
   plt.plot(b)
   plt.show()

回答by Luciano

Another way, for variety. Although this is somewhat less flexible than the others. Unfortunately, the graphs appear one above the other, rather than side-by-side, which you did request in your original question. But it is very concise.

另一种方式,多样化。虽然这比其他的灵活一些。不幸的是,这些图表一个在另一个之上,而不是并排出现,这是您在原始问题中要求的。但它非常简洁。

df.plot(subplots=True)

If the dataframe has more than the two series, and you only want to plot those two, you'll need to replace dfwith df[['korisnika','osiguranika']].

如果数据框有两个以上的系列,而您只想绘制这两个系列,则需要替换dfdf[['korisnika','osiguranika']].

回答by steven

I don't know if this is new functionality, but this will plot on separate figures:

我不知道这是否是新功能,但这将绘制在单独的数字上:

df.plot(y='korisnika')
df.plot(y='osiguranika')

while this will plot on the same figure: (just like the code in the op)

虽然这将绘制在同一个图上:(就像操作中的代码一样)

df.plot(y=['korisnika','osiguranika'])

I found this question because I was using the former method and wanted them to plot on the same figure, so your question was actually my answer.

我发现这个问题是因为我使用的是前一种方法并希望它们绘制在同一个图形上,所以你的问题实际上是我的答案。

回答by mgoldwasser

Something like this:

像这样的东西:

import matplotlib.pyplot as plt
... code for plot 1 ...
plt.show()
... code for plot 2...
plt.show()

Note that this will also work if you are using the seabornpackage for plotting:

请注意,如果您使用该seaborn包进行绘图,这也将起作用:

import matplotlib.pyplot as plt
import seaborn as sns
sns.barplot(... code for plot 1 ...) # plot 1
plt.show()
sns.barplot(... code for plot 2 ...) # plot 2
plt.show()