繁体   English   中英

如何使用python和给定数据生成图像?

[英]How to generate image by using python and given data?

我有一个像这样的数据文件:

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. 我是否需要更改数据文件? 如何?

您仅在绘图吗? PIL是图像处理模块-如果需要直方图和其他图形,则应考虑使用matplotlib

我在这里找到了一个直方图的例子。

直方图通常是在matplotlib中制作的,方法是拥有一组数据点,然后将它们分配到bin中。 一个例子是这样的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM