如何使用python和给定的数据生成图像?
问题描述:
我有一个数据文件,该文件是这样的:如何使用python和给定的数据生成图像?
1, 23%
2, 33%
3, 12%
我想用Python来生成一个直方图来表示百分比。我跟着这些命令:
from PIL import Image
img = Image.new('RGB', (width, height))
img.putdata(my_data)
img.show()
但是我得到的错误,当我把数据:SystemError: new style getargs format but argument is not a tuple.
我一定要改变我的数据文件吗?如何?
答
通常在matplotlib中创建一个直方图,具有一组数据点,然后将它们分配到分箱中。一个例子是这样的:
import matplotlib.pyplot as plt
data = [1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7]
plt.hist(data, 7)
plt.show()
你已经知道适合多大比例的数据,每个类别(虽然,我可能会指出你的百分比不添加到100 ...)。一种表示方法是制作一个列表,其中每个数据值的表示次数等于其百分比,如下所示。
data = [1]*23 + [2]*33 + [3]*12
plt.hist(data, 3)
plt.show()
hist()的第二个参数是显示的箱的数量,所以这可能是你想让它看起来很漂亮的数字。
的文档HIST()在这里找到: http://matplotlib.org/api/pyplot_api.html
http://stackoverflow.com/questions/12062920/how-do-i-create-an-image-in-pil-using-a-列表的-RGB元组 – Mani