多个循环的问题,以创建绘图,获得错误

时间:2014-06-03 20:01:50

标签: python loops matplotlib pandas

基本上我一直在尝试从数据帧创建绘图。首先,我定义数据帧(glist),然后定义y值(alist)以对照日期。它们都是单独工作但不是在这个循环中。有什么建议吗?

编辑:现在的问题是,当我打电话给他们时,第一个地块正在为所有其他地块重新制作。我试图在循环开始时使用plt.clr(),但似乎没有用。

x,y,= (0.5,-.16)
fmt = DateFormatter('%m/%d/%Y')
nlist=[]
def plots():

Totrigs,TotOrDV,TotOrH,TotGas,TotOil,TotBit,ABFl,ABOr,SKFl,SKOr,BCOr,MBFl,MBOr = dataforgraphs()
glist = [Totrigs,ABFl,ABOr,SKFl,SKOr,BCOr,MBFl,MBOr]
alist = [['Total Rigs'],['GAS','OIL','BIT'],[['HZ','DIR/VERT']],[['GAS','OIL']],[['HZ','DIR/VERT']],['OIL'],[['HZ','DIR/VERT']],[['HZ','DIR/VERT']]]

for i,k in zip(glist,alist):
    fig = i.plot(y=k,linewidth=2.0, legend=False)
    fig.patch.set_facecolor('#EEECE1')
    fig.xaxis.set_major_formatter(fmt)
    fig.legend(loc= 'upper center',bbox_to_anchor=(x,y),fancybox=True)
    nlist.append(fig)

    return (nlist)

def Totgraph():
nlist = plots()
Totrigs = nlist[0]           
plt.savefig('C:\\Python33\\XcelFiles\\Pics\\Totrigs.png',bbox_inches='tight')
plt.clf()

def ABFLgraph():
nlist = plots()
ABFl = nlist[1]
plt.title('Alberta Fluid Type')
plt.savefig('C:\\Python33\\XcelFiles\\Pics\\ABFl.png',bbox_inches='tight')

1 个答案:

答案 0 :(得分:2)

示例数据:

import pandas as pd
df = pd.DataFrame({'x':[1,2,3,4], 'y':[10,20,30,40], 'z':[100,200,300,400]})

d_list = [df, df, df, df]
y_list = [['z'], ['y'], ['y', 'z']]

您的代码应该是这样的:

x,y,= (0.5,-.16)
fmt = DateFormatter('%m/%d/%Y')
res = []
for g in glist:
    for a in alist:
        fig = g.plot(x='x', y=a, sharex=True, linewidth=2.0, legend=False))
        fig.patch.set_facecolor('#EEECE1')
        # my x axis isn't dates, but you get the picture...
        # fig.xaxis.set_major_formatter(fmt)
        fig.legend(loc= 'upper center',bbox_to_anchor=(x,y),fancybox=True)
        res.append(fig) 

您目前错误的代码:

for g, a in glist, alist:
    print g, a

无效的python。您需要在问题的代码中使用嵌套循环。或者您可以使用zip之类的内容来获取对:

for g, a in zip(glist, alist):
    print g, a