使用来自 a-for 循环的数据在 python 中创建线图
Creating a line plot in python using data from a-for loop
我有一些以前的代码可以打印出来
'The number for january is x' - 等等,整整一年。
我正在尝试使用此绘制 x 与月份的关系图:
import matplotlib.pyplot as plt
for m, n in result.items():
print 'The number for', m, "is", n
plt.plot([n])
plt.ylabel('Number')
plt.xlabel('Time (Months)')
plt.title('Number per month')
plt.show()
其中 m 是月份(也是它读取的文件名,n 是 x 值(数字)。
然而,当我 运行 这时,我只得到一张空白图表 - 我想我可能遗漏了一些重要的东西?
结果包含:
{'apr': 13, 'jun': 13, 'jul': 13, 'aug': 13, 'sep': 13, 'oct': 13} x 6 times
出于实际目的,我将每个文件的编号设置为 13,因为实际文件非常庞大
import matplotlib.pyplot as plt
import numpy as np
result = {'apr': 13, 'jun': 13, 'jul': 13, 'aug': 13, 'sep': 13, 'oct': 13}
for m, n in result.items():
print 'The number for', m, "is", n
plt.plot(result.values())
plt.ylabel('Number')
plt.xlabel('Time (Months)')
plt.title('Number per month')
plt.xticks(range(len(result)), result.keys())
plt.show()
所以我在这里所做的就是删除 for
循环之外的绘图部分。现在您将像以前一样打印结果,但是将为所有值绘制一次。
您可以使用 dict.values
从字典中取出值,在我们的例子中为我们提供了所有 13
的值。
我有一些以前的代码可以打印出来
'The number for january is x' - 等等,整整一年。
我正在尝试使用此绘制 x 与月份的关系图:
import matplotlib.pyplot as plt
for m, n in result.items():
print 'The number for', m, "is", n
plt.plot([n])
plt.ylabel('Number')
plt.xlabel('Time (Months)')
plt.title('Number per month')
plt.show()
其中 m 是月份(也是它读取的文件名,n 是 x 值(数字)。
然而,当我 运行 这时,我只得到一张空白图表 - 我想我可能遗漏了一些重要的东西?
结果包含:
{'apr': 13, 'jun': 13, 'jul': 13, 'aug': 13, 'sep': 13, 'oct': 13} x 6 times
出于实际目的,我将每个文件的编号设置为 13,因为实际文件非常庞大
import matplotlib.pyplot as plt
import numpy as np
result = {'apr': 13, 'jun': 13, 'jul': 13, 'aug': 13, 'sep': 13, 'oct': 13}
for m, n in result.items():
print 'The number for', m, "is", n
plt.plot(result.values())
plt.ylabel('Number')
plt.xlabel('Time (Months)')
plt.title('Number per month')
plt.xticks(range(len(result)), result.keys())
plt.show()
所以我在这里所做的就是删除 for
循环之外的绘图部分。现在您将像以前一样打印结果,但是将为所有值绘制一次。
您可以使用 dict.values
从字典中取出值,在我们的例子中为我们提供了所有 13
的值。