简体   繁体   中英

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. 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. 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 .

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. 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...). 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.

Documentation for hist() is found here: http://matplotlib.org/api/pyplot_api.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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