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.

8
  • 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. Commented Mar 2, 2013 at 8: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 Commented Mar 2, 2013 at 8: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. Commented Mar 2, 2013 at 8: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 Commented Mar 2, 2013 at 8: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() Commented Mar 2, 2013 at 8:54

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

it works except that I see three colours instead of just two. Too much hassle for such a simple task.
@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.
@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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.