Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib histograms and labels

I am plotting a histogram with two sets of data and want to include a key. I have found an example that uses the label command, but when I use this it doesn't work. The histogram appears correctly, but there is no key. My code is.

H1 = <some data>
S1 = <some other data>

hist([H1,S1], bins=25, range=(10,30), align=('mid'), color=['green', 'orange'], label=['Actual H band', 'Actual IRAC2 band'])
title("Actual observed magnitudes of sources in H and IRAC2")
xlabel("Magnitude")
ylabel("Frequency")

show()

Any suggestions?

like image 907
nancyh Avatar asked Jun 06 '13 09:06

nancyh


People also ask

How do you show data labels in a histogram in Python?

Create a dataset using numpy library so that we can plot it. Create a histogram using matplotlib library. To give labels use set_xlabel() and set_ylabel() functions. We add label to each bar in histogram and for that, we loop over each bar and use text() function to add text over it.

How do you label a histogram?

Look at a blank graph and identify its x and y-axis. The x-axis always runs horizontal -- along the bottom of the histogram and the y-axis runs vertical -- or lengthwise. Label the y-axis to identify what you are measuring.

How do I display the count over the bar in Matplotlib histogram?

To display the count over the bar in matplotlib histogram, we can iterate each patch and use text() method to place the values over the patches.

How do you do a histogram in Matplotlib?

Create Histogram In Matplotlib, we use the hist() function to create histograms. The hist() function will use an array of numbers to create a histogram, the array is sent into the function as an argument.

How to add labels to each bar in histogram in Matplotlib?

Create a histogram using matplotlib library. To give labels use set_xlabel () and set_ylabel () functions. We add label to each bar in histogram and for that, we loop over each bar and use text () function to add text over it. We also calculate height and width of each bar so that our label don’t coincide with each other.

How to add labels to a histogram using NumPy?

Create a dataset using numpy library so that we can plot it. Create a histogram using matplotlib library. To give labels use set_xlabel () and set_ylabel () functions. We add label to each bar in histogram and for that, we loop over each bar and use text () function to add text over it.

How to plot a 2D histogram in Matplotlib?

To plot a 2D histogram, one only needs two vectors of the same length, corresponding to each axis of the histogram. fig, ax = plt.subplots(tight_layout=True) hist = ax.hist2d(x, y) Customizing your histogram ¶ Customizing a 2D histogram is similar to the 1D case, you can control visual components such as the bin size or color normalization.

What is a histogram in Python?

A histogram is a plot of the frequency distribution of numeric array by splitting it to small equal-sized bins. If you want to mathemetically split a given array to bins and frequencies, use the numpy histogram () method and pretty print it like below.


1 Answers

You have to include a legend call:

H1 = <some data>
S1 = <some other data>

hist([H1,S1], bins=25, range=(10,30), align=('mid'), color=['green', 'orange'], label=['Actual H band', 'Actual IRAC2 band'])
title("Actual observed magnitudes of sources in H and IRAC2")
xlabel("Magnitude")
ylabel("Frequency")
legend()

show()
like image 81
sodd Avatar answered Nov 23 '22 18:11

sodd