2

I need to plot several histograms on the same plot. I like the display the following code generates:

import random
import numpy
from matplotlib import pyplot

x = [random.gauss(3,1) for _ in range(400)]
y = [random.gauss(4,2) for _ in range(400)]

bins = numpy.linspace(-10, 10, 100)

pyplot.hist(x, bins, alpha=0.5)
pyplot.hist(y, bins, alpha=0.5)
pyplot.show()

This code was mentioned on this page:Plot two histograms at the same time with matplotlib Basically I am having trouble plotting the same kind of histograms but for data that looks like:

y1=[20,33,54,34,22]
x1=[0,2,4,6,8]
y2=[28,31,59,14,12]
x2=[0,2,4,6,8]

Using the aforementioned code I could not get the y axis to go above 2.0 strange but I must be making a foolish mistake.

Thanks.

Community
  • 1
  • 1
shokan
  • 33
  • 4
  • Really hard to guess, since this code should work. I would try eliminating different plots (comment out y or x plot) and see if you can narrow down the source of the problem. – Radio- Mar 02 '13 at 08:08
  • Maybe you should look at the definition of hist. If your bins are those in x1 it is normal you get nothing from the values in y1 – joaquin Mar 02 '13 at 08:51
  • I have a feeling this only works with randomly generated data(as seen from the examples on this site) but surely that can't be the case. – shokan Mar 02 '13 at 08:51
  • No, random data is used as an example. hist works for any kind of integer or float data. The problem is you are not applying hist correctly. for your data probably you want to use a bar plot – joaquin Mar 02 '13 at 08:53
  • @joaquin I am unable to get the correct histogram for even a single data set: import pyfits, numpy, math, pylab, scipy.optimize, time import matplotlib import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from scipy import optimize from scipy.optimize import leastsq from lmfit import minimize, Parameters from numpy import * from matplotlib import pyplot x = numpy.array([11,2,3,14,5]) #y = numpy.array([24,33,56,21,99]) print(x) #print(y) bins =[0,2,4,6,8] pyplot.hist(x,bins) #pyplot.hist(y, bins) pyplot.show() – shokan Mar 02 '13 at 08:54
  • I need the same graphic as the one in the first code since I want to be able to see the histograms with transparency one on top of each other. – shokan Mar 02 '13 at 08:59
  • Which one is your dataset ? what y and x mean?. hist calculates and represents the frecuency of a series of numbers taken in some intervals (the bins). If your intervals go from 0 to 8 it is normal it doesnt show anything if your series contains numbers from 20 to 54 – joaquin Mar 02 '13 at 09:00
  • Okay joaquin I think you are correct. I did make use of plt.bar and not .hist, but I got distracted due to the transparency feature available with hist. All I need now is a way to display my bar charts on top of each other with each one still visible as in the link I first attached. Would you know how to do that? – shokan Mar 02 '13 at 09:10

1 Answers1

1

Probably you are looking for this:

 pyplot.bar(x2,y2, color='b', width=2, alpha=0.5)
 pyplot.bar(x1,y1, color='r', width=2, alpha=0.5)
 pyplot.show()

enter image description here

joaquin
  • 82,968
  • 29
  • 138
  • 152
  • it works except that I see three colours instead of just two. Too much hassle for such a simple task. – shokan Mar 02 '13 at 10:02
  • @shokan what do you mean 'three colors'? If you overlay two things with `alpha < 1` you _must_ get three colors. The `alpha` kwarg is an `artist` kwarg so all of the plotting functions _should_ respect it. – tacaswell Mar 02 '13 at 17:09
  • @tcaswell but you see when I include legends on to the plot it only serves as a key for the two data sets while you see three colours on the plot. Hence why is it that in some places pink and blue are used and in other you have a pink and a combination of pink and blue used. It is hard to differentiate. – shokan Mar 04 '13 at 08:02
  • @shokan You are overlaying two transparent colors. If you don't want this, set `alpha=1`, but then you won't be able to see the back bar when it is smaller. – tacaswell Mar 04 '13 at 16:27