How to generate image by using python and given data? - python

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?

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

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.

Related

Adding multiple images to a matplotlib subplot?

I am trying to make a matplottlib plot using some image data I have in numpy format, and was wondering if someone would be able to advise me on the best way to approach displaying multiples of these images within the boundaries of one subplot?
For example, using the following code...
n_samples = 10
sample_imgs, min_index = visualise_n_way(n_samples)
print(min_index)
print(sample_imgs.shape)
print(sample_imgs[0].shape)
print(x_train_w)
print(x_train_h)
img_matrix = []
for index in range(1, len(sample_imgs)):
img_matrix.append(np.reshape(sample_imgs[index], (x_train_w, x_train_h)))
img_matrix = np.asarray(img_matrix)
img_matrix = np.vstack(img_matrix)
f, ax = plt.subplots(1, 3, figsize = (10, 12))
f.tight_layout()
ax[0].imshow(np.reshape(sample_imgs[0], (x_train_w, x_train_h)),vmin=0, vmax=1,cmap='Greys')
ax[0].set_title("Test Image")
ax[1].imshow(img_matrix ,vmin=0, vmax=1,cmap='Greys')
ax[1].set_title("Support Set")
ax[2].imshow(np.reshape(sample_imgs[min_index], (x_train_w, x_train_h)),vmin=0, vmax=1,cmap='Greys')
ax[2].set_title("Image most similar to Test Image in Support Set")
I get the following image and output
1
(11, 784)
(784,)
28
28
Matplotlib Output
What I would like to do however is to have the second subplot, the one displaying img_matrix, to be the same size as the two either side of it, creating a grid of the images. Sort of like this
sketch.
I am at a loss as to how to do this however. I believe I may need to use something such as a gridspace, but I'm finding the documentation hard to follow for what I want to do.
Any help is greatly appreciated!

I want to calculate occurence of data over a range (period of 10) in Python

I have data in numeric form so I want to calculate occurrence of data in a range (period of 10). I have created a Python script. The
original script is very long because of large dataset so I am putting here a sample code.
In the actual code malware_opcd_frq list size is approx 19000 and bins list [0,11,21,31...........13991,14000]
opcode_frequency.py
import numpy as np
malware_opcd_frq = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,4,4,5,5,6,6,6,19,25,26,28,29,35,41,43,43,49,54,57,60,71,78,79,81,81,92,99,99,105,107,109,119,129,134,142,142,145,146,150,158,166,166,171,172,173,180,183,186,187,191,191,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,195,198,199,203,209,209,217,217,220,220,225,226,226,226,226,226,226,226,226,226,226,226,228,234,234,235,236,236,236,237,237,239,240,241,241,243,244,244,245,245,245,245,246,247,248,250,253,256,257,258,259,259,260,262,264,264,267,267,267,269,270,270,272,273,274,275,278,279,284,295,295,300])
frq = np.histogram(malware_opcd_frq, bins= [0,11,21,31,41,51,61,71,81,91,101,111,121,131,141,151,161,171,181,191,201,211,221,231,241,251,261,271,281,291,300])
print frq
so after execution of the actual code which I have gives output like this
(array([29, 1, 4, ..., 5, 9, 7]), array([ 0, 11, 21, ..., 13981, 13991, 14000]))
In the above output but I need full output — but I'm not getting it. Please explain what I need to do.
The "..." that you see are just a way to tell you that the variable is very big. Therefore you see just a small part of your variable.
frq is a tuple, where frq[0] are the amount of values in each bin and frq[1] are the bins that you use.
You can make the plot of all your data with:
import matplotlib.pyplot as plt
plt.plot(frq[1][1::],frq[0])

Python matplotlib - setting x-axis scale

I have this graph displaying the following:
plt.plot(valueX, scoreList)
plt.xlabel("Score number") # Text for X-Axis
plt.ylabel("Score") # Text for Y-Axis
plt.title("Scores for the topic "+progressDisplay.topicName)
plt.show()
valueX = [1, 2, 3, 4] and
scoreList = [5, 0, 0, 2]
I want the scale to go up in 1's, no matter what values are in 'scoreList'. Currently get my x-axis going up in .5 instead of 1s.
How do I set it so it goes up only in 1?
Just set the xticks yourself.
plt.xticks([1,2,3,4])
or
plt.xticks(valueX)
Since the range functions happens to work with integers you could use that instead:
plt.xticks(range(1, 5))
Or be even more dynamic and calculate it from the data:
plt.xticks(range(min(valueX), max(valueX)+1))
Below is my favorite way to set the scale of axes:
plt.xlim(-0.02, 0.05)
plt.ylim(-0.04, 0.04)
Hey it looks like you need to set the x axis scale.
Try
matplotlib.axes.Axes.set_xscale(1, 'linear')
Here's the documentation for that function

Python version of h=area() in Matlab

I asked a related question yesterday and fortunately got my answer from jlarsch quickly. But now I am stuck with the next part, which starts with the h=area() line. I'd like to know the python version of the area() function, via which I will be able to set the colors. Could someone shed me some light again? Thanks much in advance.
...
Subplot (2,1,1);
H = plot (rand(100,5));
C = get (H, 'Color')
H = area (myX, myY);
H(1).FaceColor = C(1);
H(2).FaceColor = C(2);
Grid on;
...
The pretty much exact equivalent of MATLAB's Area plot is matplotlib's stackplot. Here is the first MATLAB example from the above link reproduced using matplotlib:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(4)
y = [[1, 3, 1, 2],
[5, 2, 5, 6],
[3, 7, 3, 1]]
plt.stackplot(x, y)
plt.show()
And here is the result:
You might be looking for pygame.draw.polygon(), which can fill a polygon defined by an arbitrary array of points.
You probably want plt.fill().
A huge amount of graph types at Matplotlib Gallery

Python: how to make a histogram with given bins and binned data [duplicate]

This question already has answers here:
Plotting a histogram from pre-counted data in Matplotlib
(6 answers)
Closed 8 years ago.
I would like to make a histogram with binned data with Python, but I didn't figure out how to make one. I didn't find any documentation (or I Googled wrong), but I've tried this:
import pylab as plb
a = [1, 2, 3, 4, 5, 6] # my histogram bins
b = [1, 4, 6, 1, 3, 7] # my data
plb.hist(b, bins = a)
plb.show()
and related alternatives, and of course it doesn't work.
The example you give runs just fine on my PC, granted it is not looking very nice. A good place to search for recipes common graphs is the gallery of Matplotlib: http://matplotlib.org/gallery.html. If you want to get more information about what arguments the functions use, you can take a look at the documentation (http://matplotlib.org/api/pyplot_api.html?highlight=hist#matplotlib.pyplot.hist)
If you take a look at that gallery and search for histogram, you will find this example which fits your needs:
http://matplotlib.org/examples/statistics/histogram_demo_histtypes.html

Categories

Resources