使用循环绘制n个图表Python [英] Use a loop to plot n charts Python

查看:157
本文介绍了使用循环绘制n个图表Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据,这些数据是使用pandas数据框加载到python中的.我想做的是创建一个循环,该循环将为它们自己框架中的所有元素(而不是所有元素)打印一个图.我的数据保存在以这种方式构造的excel文件中:

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:

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)

以上内容并不是我想要的,它源于我对Pandas,MatplotLib等的不熟悉,尽管查看了一些文档,但对我来说似乎甚至不需要matplotlib(问题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)

所以A)如何为df中的每一列生成数据图 和B)我需要使用matplotlib还是应该仅使用熊猫来完成所有工作?

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?

谢谢

推荐答案

好,因此创建多个绘图的最简单方法是:

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()

请注意,您每次都需要创建一个figure,否则将在创建的第一个图中绘制pyplot.

Note that you need to create a figure every time or pyplot will plot in the first one created.

如果要创建多个数据系列,您需要做的是:

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')

您可以通过具有颜色列表(如['r','g','b','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):

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()

希望这会有所帮助.如果有的话,matplotlib的文档页面很好,其中包含许多

Hope this helps. If anything matplotlib has a very good documentation page with plenty of examples.

2019年12月17日:添加了plt.show()plt.figure()调用以澄清故事的这一部分.

17 Dec 2019: added plt.show() and plt.figure() calls to clarify this part of the story.

这篇关于使用循环绘制n个图表Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆