Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用多个循环创建绘图时出现问题,导致错误_Python_Loops_Matplotlib_Pandas - Fatal编程技术网

Python 使用多个循环创建绘图时出现问题,导致错误

Python 使用多个循环创建绘图时出现问题,导致错误,python,loops,matplotlib,pandas,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,To

基本上,我一直在尝试从数据帧创建绘图。首先,我定义数据帧(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')
样本数据:

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

你在哪一行上出错?我想
fig=I.plot(y=alist[x],sharex=True,linewidth=2.0,legend=False)
是你的问题。在本例中,您的
y=alist[x]
是一个列表。我认为y不能是一个列表,这就是为什么它会给你这个错误。我应该将列表转换成什么,以便它将其作为y值的列标签来读取?@Elias
plot
方法在
DataFrame
上获取一个参数列表(同样
sharex
默认为
True
)。没有更多的信息,很难回答。错误在哪一行?什么是
y
fmt
?是否所有的
数据帧都具有相同的列和类型?为什么要使用
x
而不是直接使用
k
编制索引?哪个错误?
值错误
?因为这不是有效的语法,但也不是您代码中的内容…
您当前出错的代码:d,y在d_列表中,y_列表:print d,y
我在他的代码中没有看到这一点?zip工作了!!谢谢你,伙计。基本上,问题是两者都需要在同一时间通过循环读取,因为我创建了绘图,同时k有它要使用的列。是的,如果他显示错误的实际行,那将是很好的。美好的job@Justin有重印错误。我想我知道它为什么会发生,但不知道如何修复它。我的尝试是徒劳的。我编辑了最初的帖子。@carevans88尽管我很了不起,但我的直觉还不足以理解这一点。请恢复您的编辑,接受我的上述答案,并提出一个新的问题,如果需要,链接到此问题。确保包含所有相关信息,包括准确的错误。但是,在您的编辑中,
plt
不存在。您需要引用返回的对象,例如
Totrigs.savefig