简体   繁体   English

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

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

I have one data file which is like this: 我有一个像这样的数据文件:

1, 23%
2, 33%
3, 12%

I want to use python to generate one histogram to represent the percentage. 我想使用python生成一个直方图来表示百分比。 I followed these command: 我遵循以下命令:

from PIL import Image
img = Image.new('RGB', (width, height))
img.putdata(my_data)
img.show()

However I got the error when I put the data: SystemError: new style getargs format but argument is not a tuple. 但是,当我放置数据时出现了错误: SystemError: new style getargs format but argument is not a tuple. Do I have to change my data file? 我是否需要更改数据文件? and How? 如何?

Are you graphing only? 您仅在绘图吗? PIL is an image processing module - if you want histograms and other graphs you should consider matplotlib . PIL是图像处理模块-如果需要直方图和其他图形,则应考虑使用matplotlib

I found an example of a histogram here . 我在这里找到了一个直方图的例子。

A histogram is usually made in matplotlib by having a set of data points and then assigning them into bins. 直方图通常是在matplotlib中制作的,方法是拥有一组数据点,然后将它们分配到bin中。 An example would be this: 一个例子是这样的:

import matplotlib.pyplot as plt

data = [1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7]
plt.hist(data, 7)
plt.show()

You already know what percentage of your data fits into each category (although, I might point out your percentages don't add to 100...). 您已经知道数据适合每个类别的百分比(尽管我可能会指出您的百分比未加到100 ...)。 A way to represent this is to to make a list where each data value is represented a number of times equal to its percentage like below. 表示此问题的一种方法是制作一个列表,其中每个数据值的表示次数等于其百分比,如下所示。

data = [1]*23 + [2]*33 + [3]*12
plt.hist(data, 3)
plt.show()

The second argument to hist() is the number of bins displayed, so this is likely the number you want to make it look pretty. hist()的第二个参数是显示的垃圾箱数量,因此这可能是您想要使其看起来漂亮的数字。

Documentation for hist() is found here: http://matplotlib.org/api/pyplot_api.html hist()的文档位于: http : //matplotlib.org/api/pyplot_api.html

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

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