pandas 使用循环绘制 n 个图表 Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19189488/
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
Use a loop to plot n charts Python
提问by MCP_infiltrator
I have a set of data that I load into python using a pandas dataframe. What I would like to do is create a loop that will print a plot for all the elements in their own frame, not all on one. My data is in an excel file structured in this fashion:
我有一组使用 Pandas 数据框加载到 python 中的数据。我想要做的是创建一个循环,它将为自己框架中的所有元素打印一个图,而不是全部打印在一个图上。我的数据位于以这种方式构建的 excel 文件中:
Index | DATE | AMB CO 1 | AMB CO 2 |...|AMB CO_n | TOTAL
1 | 1/1/12| 14 | 33 |...| 236 | 1600
. | ... | ... | ... |...| ... | ...
. | ... | ... | ... |...| ... | ...
. | ... | ... | ... |...| ... | ...
n
This is what I have for code so far:
到目前为止,这是我的代码:
import pandas as pd
import matplotlib.pyplot as plt
ambdf = pd.read_excel('Ambulance.xlsx',
sheetname='Sheet2', index_col=0, na_values=['NA'])
print type(ambdf)
print ambdf
print ambdf['EAS']
amb_plot = plt.plot(ambdf['EAS'], linewidth=2)
plt.title('EAS Ambulance Numbers')
plt.xlabel('Month')
plt.ylabel('Count of Deliveries')
print amb_plot
for i in ambdf:
print plt.plot(ambdf[i], linewidth = 2)
I am thinking of doing something like this:
我正在考虑做这样的事情:
for i in ambdf:
ambdf_plot = plt.plot(ambdf, linewidth = 2)
The above was not remotely what i wanted and it stems from my unfamiliarity with Pandas, MatplotLib etc, looking at some documentation though to me it looks like matplotlib is not even needed (question 2)
以上并不是我想要的,它源于我对 Pandas、MatplotLib 等的不熟悉,查看了一些文档,但在我看来似乎甚至不需要 matplotlib(问题 2)
So A) How can I produce a plot of data for every column in my df and B) do I need to use matplotlib or should I just use pandas to do it all?
所以 A) 如何为我的 df 和 B) 中的每一列生成数据图,我是否需要使用 matplotlib 还是应该只使用 Pandas 来完成这一切?
Thank you,
谢谢,
回答by Aleksander Lidtke
Ok, so the easiest method to create several plots is this:
好的,所以创建多个图的最简单方法是:
import matplotlib.pyplot as plt
x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
for i in range(len(x)):
plt.figure()
plt.plot(x[i],y[i])
# Show/save figure as desired.
plt.show()
# Can show all four figures at once by calling plt.show() here, outside the loop.
#plt.show()
Note that you need to create a figure
every time or pyplot
will plot in the first one created.
请注意,您需要figure
每次都创建一个或pyplot
将在创建的第一个中绘制。
If you want to create several data series all you need to do is:
如果您想创建多个数据系列,您需要做的就是:
import matplotlib.pyplot as plt
plt.figure()
x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]]
plt.plot(x[0],y[0],'r',x[1],y[1],'g',x[2],y[2],'b',x[3],y[3],'k')
You could automate it by having a list of colours like ['r','g','b','k']
and then just calling both entries in this list and corresponding data to be plotted in a loop if you wanted to. If you just want to programmatically add data series to one plot something like this will do it (no new figure is created each time so everything is plotted in the same figure):
您可以通过拥有一个颜色列表来自动化它['r','g','b','k']
,然后只需调用此列表中的两个条目,并根据需要在循环中绘制相应的数据。如果您只想以编程方式将数据系列添加到一个图中,这样的事情就可以做到(每次都不会创建新图形,因此所有内容都绘制在同一个图形中):
import matplotlib.pyplot as plt
x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]]
colours=['r','g','b','k']
plt.figure() # In this example, all the plots will be in one figure.
for i in range(len(x)):
plt.plot(x[i],y[i],colours[i])
plt.show()
Hope this helps. If anything matplotlib has a very good documentation pagewith plenty of examples.
希望这可以帮助。如果有的话,matplotlib 有一个非常好的文档页面,其中包含大量示例。
17 Dec 2019: added plt.show()
and plt.figure()
calls to clarify this part of the story.
2019 年 12 月 17 日:添加plt.show()
并plt.figure()
呼吁澄清故事的这一部分。
回答by G M
Use a dictionary!!
使用字典!!
You can also use dictionaries that allows you to have more control over the plots:
您还可以使用字典来更好地控制绘图:
import matplotlib.pyplot as plt
# plot 0 plot 1 plot 2 plot 3
x=[[1,2,3,4],[1,4,3,4],[1,2,3,4],[9,8,7,4]]
y=[[3,2,3,4],[3,6,3,4],[6,7,8,9],[3,2,2,4]]
plots = zip(x,y)
def loop_plot(plots):
figs={}
axs={}
for idx,plot in enumerate(plots):
figs[idx]=plt.figure()
axs[idx]=figs[idx].add_subplot(111)
axs[idx].plot(plot[0],plot[1])
return figs, axs
figs, axs = loop_plot(plots)
Now you can select the plot that you want to modify easily:
现在您可以轻松选择要修改的图:
axs[0].set_title("Now I can control it!")
Of course, is up to you to decide what to do with the plots. You can either save them to disk figs[idx].savefig("plot_%s.png" %idx)
or show them plt.show()
. Use the argument block=False
onlyif you want to pop up all the plots together (this could be quite messy if you have a lot of plots). You can do this inside the loop_plot
function or in a separate loop using the dictionaries that the function provided.
当然,由您决定如何处理这些情节。您可以将它们保存到磁盘figs[idx].savefig("plot_%s.png" %idx)
或显示它们plt.show()
。block=False
仅当您想将所有图一起弹出时才使用该参数(如果您有很多图,这可能会非常混乱)。您可以在loop_plot
函数内部或使用函数提供的字典在单独的循环中执行此操作。
回答by DrM
Here are two examples of how to generate graphs in separate windows (frames), and, an example of how to generate graphs and save them into separate graphics files.
下面是如何在单独的窗口(框架)中生成图形的两个示例,以及如何生成图形并将它们保存到单独的图形文件中的示例。
Okay, first the on-screen example. Notice that we use a separate instance of plt.figure(), for each graph, with plt.plot(). At the end, we have to call plt.show()to put it all on the screen.
好的,首先是屏幕上的示例。请注意,对于每个图形,我们使用plt.figure()的单独实例和plt.plot()。最后,我们必须调用plt.show()将其全部显示在屏幕上。
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace( 0,10 )
for n in range(3):
y = np.sin( x+n )
plt.figure()
plt.plot( x, y )
plt.show()
Another way to do this, is to use plt.show(block=False) inside the loop:
另一种方法是在循环内使用 plt.show(block=False) :
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace( 0,10 )
for n in range(3):
y = np.sin( x+n )
plt.figure()
plt.plot( x, y )
plt.show( block=False )
Now, let's generate the graphs and instead, write them each to a file. Here we replace plt.show(), with plt.savefig( filename ). The difference from the previous example is that we don't have to account for ''blocking'' at each graph. Note also, that we number the file names. Here we use %03d so that we can conveniently have them in number order afterwards.
现在,让我们生成图形,而是将它们每个都写入一个文件。在这里,我们将 plt.show() 替换为plt.savefig( filename )。与前一个示例的不同之处在于,我们不必在每个图中都考虑“阻塞”。另请注意,我们对文件名进行编号。在这里我们使用 %03d 以便我们可以方便地将它们按编号顺序排列。
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace( 0,10 )
for n in range(3):
y = np.sin( x+n )
plt.figure()
plt.plot( x, y )
plt.savefig('myfilename%03d.png'%(n))
回答by Rupanjan Nayak
We can create a for loop and pass all the numeric columns into it. The loop will plot the graphs one by one in separate pane as we are including plt.figure()into it.
我们可以创建一个 for 循环并将所有数字列传递给它。当我们将plt.figure()包含在其中时,循环将在单独的窗格中一一绘制图形 。
import pandas as pd
import seaborn as sns
import numpy as np
numeric_features=[x for x in data.columns if data[x].dtype!="object"]
#taking only the numeric columns from the dataframe.
for i in data[numeric_features].columns:
plt.figure(figsize=(12,5))
plt.title(i)
sns.boxplot(data=data[i])