Histogram outlined by added edgecolor
Asked Answered
S

2

94

I have plotted a histogram and was expecting to see the outlines of my bars but this is not the case.

enter image description here

I'm using the following code:

import matplotlib.pyplot as plt
from numpy.random import normal
gaussian_numbers = normal(size=1000)
plt.hist(gaussian_numbers)
plt.title("Gaussian Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

How do I show the outline of the bars?

Shackleford answered 11/3, 2017 at 22:58 Comment(2)
for me running your code, the lines are there. Did you modify the default line width? Second guess, the edgecolor could be the same as the bar color. (try calling: plt.hist(gaussian_numbers, linewidth=1, edgecolor='r')Schorl
The reason, some people see the outlines by default and others don't, is that they use different versions of matplotlib. The questioner uses matplotlib 2.0 while Joma and @James use matplotlib 1.5. Using edgecolor = "k" indeed brings the lines back in matplotlib 2.0.Locke
W
180

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)

enter image description here

Wollongong answered 11/3, 2017 at 23:9 Comment(4)
Also see @Locke ' s comment why that is so.Qnp
How do you know about edgecolor? In the documentation there is no mention to it.Goebel
In your documentation link, the last section is "Other Parameters" which contains**kwargs. The link next to kwargs is the patch documentation. This indicates that any parameter that can be applied to a patch can be passed as a key word argument to hist, including edgecolorWollongong
Yeah, **kawrgs is a group of parameters that is common to ALL graphs in matplotlib. They do this so they don't have to copy-paste them all to every page.Imprudent
A
0

If you want to show black bar edgecolors for all histograms in the current runtime (e.g. current Jupyter kernel), you can do so by using rcParams. Since histograms are actually bar charts under the hood (calls .bar) which in turn adds Rectangle patches to the Axes, the key to set to True is 'patch.force_edgecolor'.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['patch.force_edgecolor'] = True   # show edgecolor

gaussian_numbers = np.random.default_rng(0).normal(0, 1, 1000)
plt.hist(gaussian_numbers);

img

On a related note, even though edgecolor, linewidth, alpha etc. are not listed among .hist parameters, they can be found among the Rectangle properties (because these parameters change how the bars are drawn). This is useful to know especially if you are using another library that uses matplotlib to plot histograms such as pandas, seaborn etc. Then pass the patch parameters to change facecolor, alpha, edgecolor etc.

s = pd.Series(gaussian_numbers)
s.plot.hist(ec='k', alpha=0.5, fc='r')   # light-red bars with black outline

In the opposite direction, if you don't want to show the bar outlines, pass ec='none':

plt.hist(gaussian_numbers, ec='none');

This will not draw edgecolors no matter what the rcParams settings are.

Ahmed answered 1/11, 2023 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.