histogram
matplotlib histogram: how to display the count over the bar?
New in matplotlib 3.4.0 There is a new plt.bar_label method to automatically label bar containers. plt.hist returns the bar container(s) as the third output: data = np.random.default_rng(123).rayleigh(1, 70) counts, edges, bars = plt.hist(data) # ^ plt.bar_label(bars) If you have a grouped or stacked histogram, bars will contain multiple containers (one per group), so iterate: fig, … Read more
How to center labels in histogram plot
The other answers just don’t do it for me. The benefit of using plt.bar over plt.hist is that bar can use align=’center’: import numpy as np import matplotlib.pyplot as plt arr = np.array([ 0., 2., 0., 0., 0., 0., 3., 0., 0., 0., 0., 0., 0., 0., 0., 2., 0., 0., 0., 0., 0., 1., … Read more
How to add edge color to a histogram
As part of the update to matplotlib 2.0 the edges on bar plots are turned off by default. However, this behavior can be globally changed with rcParams. plt.rcParams[“patch.force_edgecolor”] = True Alternatively, set the edgecolor / ec parameter in the plot call, and potentially increase the linewidth / lw parameter. Tested in python 3.11.4, pandas 2.0.3, … Read more
Histogram outlined by added edgecolor
It looks like either your linewidth was set to zero or your edgecolor was set to ‘none’. Matplotlib changed the defaults for these in 2.0. Try using: plt.hist(gaussian_numbers, edgecolor=”black”, linewidth=1.2)
Plot two histograms on single chart
Here you have a working example: 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, label=”x”) pyplot.hist(y, bins, alpha=0.5, label=”y”) pyplot.legend(loc=”upper right”) pyplot.show()
Setting a relative frequency in a matplotlib histogram
Because normed option of hist returns the density of points, e.g dN/dx What you need is something like that: # assuming that mydata is an numpy array ax.hist(mydata, weights=np.zeros_like(mydata) + 1. / mydata.size) # this will give you fractions
Matplotlib histogram with collection bin for high values
Numpy has a handy function for dealing with this: np.clip. Despite what the name may sound like, it doesn’t remove values, it just limits them to the range you specify. Basically, it does Artem’s “dirty hack” inline. You can leave the values as they are, but in the hist call, just wrap the array in … Read more