user58925
user58925

Reputation: 1633

How to set color in matplotlib histograms

I am plotting a histogram using Matplotlib. I would like the color of the histogram to be "sky blue". But the data overlaps, and produces a histogram which is nearly black in color.

plt.hist(data, color = "skyblue")

Below is how the histogram looks. As you can see, even though I specified the color to be "Skyblue, the histogram on the right is nearly black.

enter image description here

Upvotes: 23

Views: 175524

Answers (2)

cottontail
cottontail

Reputation: 23361

This particular problem (where edgecolor doesn't match the bar color) doesn't exist anymore (at least as of matplotlib 3.0) because unless edgecolor matches the fill color by default.

Anyway, a faster way to plot a histogram (where edgecolor doesn't matter) is to plot the outline and fill it.

data = (np.random.randn(10000)-2.5)/4
plt.hist(data, bins=50, histtype='step', fill=True, color='skyblue');

Another way is to precompute the histogram using np.histogram, plot the outline using plt.stairs() and fill it.

plt.stairs(*np.histogram(data, 50), fill=True, color='skyblue')

Both options are much faster than a bare plt.hist() call especially if the number of bins is high.

plot

Upvotes: 0

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339610

The reason for the histogram to look black is that the bars' surrounding lines (which are black) take most of the space.

Options would be to get rid of the edges by setting the linewidth to zero:

plt.hist(data, color = "skyblue", lw=0)

and/or to set the edgecolor to the same color as the bars itself

plt.hist(data, color = "skyblue", ec="skyblue")

Upvotes: 34

Related Questions