我有4个不同的csv
文件(从1个xlsm工作簿中提取)。我的绘图功能有点复杂,但每个文件都可以完美工作。如果我尝试用循环绘制所有4个文件,我总是得到
ValueError:数组的长度必须相同
然后该函数将绘制列表中的第一个元素。我想为csv列表中的每个文件绘制带有子图的相同plt.figure。我认为脚本正在尝试在一个图上绘制所有不同的csv文件(这些csv文件具有相同的结构但行数不同)
def plotMSN(data):
csv = data
#csv = "LL.csv" (this is working perfect)
day = plotday
....
#this is not working
csvlist = ["LL.csv","UL.csv","UR.csv","LR.csv"]
for i in csvlist:
plotMSN(i)
time.sleep(5)
答案 0 :(得分:1)
如果您未设置子图,它们将在同一图上绘制所有图。有多种方法可以执行此操作,但是我通常使用的方法是无花果axis_array
import matplotlib.pyplot as plt
def plotMSN(i):
#whatever you're doing in here
ax.plot() #plot it on its own axis, this will reference the one you're on in your loop
fig, axis_array = plt.subplots(len(csvlist), 1, subplot_kw = {'aspect':1}) #this will set up subplots that are arranged vertically
for i, ax in zip(csvlist, axis_array):
plotMSN(i)
ETA:
OP显然想绘制每个文件并使用该功能。为此,OP需要修改:
def plotMSN(i):
#determine an appropriate name either in this function or in the loop that calls it
#plotting stuff here
fig.savefig(new_filename)
plt.close() # this prevents it from using the same instance over and over.