我试图为字母表中的每个字母绘制一个图表(仅用2个测试)。我从plot_letter func返回的数据是('LETTER',[年份范围],[数据值每年一个])。最后我的for循环调用func,检索数据并为每个创建单独的绘图。我如何将所有图表放在一个图上?
import csv
import matplotlib.pyplot as plt
start = 1880
stop = 1999
years = range(start, stop+1)
alphabet=['A','C']#,'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
def plot_letter(i):
year_values = []
for year in years:
filename = 'yob' + str(year) + '.txt'
z = open(filename)
c = 0
d = 0
for line in z:
y = line.strip().split(',')
y.remove(y[1])
c += int(y[1])
if i in y[0]:
d += int(y[1])
year_values.append(float(d)/float(c))
return i,years, year_values
for i in alphabet:
data = zip(plot_letter(i)[1], plot_letter(i)[2])
x_val = [x[0] for x in data]
y_val = [x[1] for x in data]
plt.plot(x_val, y_val)
plt.show()
答案 0 :(得分:0)
这段代码:
for i in alphabet:
data = zip(plot_letter(i)[1], plot_letter(i)[2])
x_val = [x[0] for x in data]
y_val = [x[1] for x in data]
plt.plot(x_val, y_val)
plt.show()
多次调用show()
。
如果你想要一个有多行的图,只需将它移出循环:
for i in alphabet:
data = zip(plot_letter(i)[1], plot_letter(i)[2])
x_val = [x[0] for x in data]
y_val = [x[1] for x in data]
plt.plot(x_val, y_val)
plt.show()