I know the dashes length and gap size can be set in plt.plot but is there a way to do so in plt.hist? This is what my command looks like:
plt.hist(x, bins=5, range=(7.,11.), facecolor='None', linestyle='dashed', linewidth=2, normed=1)
-
Setting of line styles has been unified on master. – tacaswell Jul 02 '15 at 19:06
2 Answers
Simply read up on the official documentation:
set_dashes
is a function that takes a sequence of on and off lengths in points.
So set_dashes((3,3))
should produce something different then set_dashes((15,15))
.
Now, for hist
that won't really work, since setting the line properties, at best, will change the appearance of the outline.
What you can do instead is
- use
numpy
'shistogram
function; it's used by pyplot'shist
, anyway, and then - plot the results using
stem
.

- 32,488
- 9
- 84
- 95

- 34,677
- 4
- 53
- 94
-
`set_dashes()` works with something like `lines, = plt.plot(x)` and then `lines.set_dashes(...)`. But the `hist` function does not allow to store the plot into a variable. Or does it? – amid Jul 02 '15 at 13:09
-
@tcaswell: um, no. actually, using stem would give you the ability to plot lines instead of rectangles. – Marcus Müller Jul 03 '15 at 14:28
-
@MarcusMüllerꕺꕺ: not sure how to get `stem` to plot a histogram. Is that possible? I checked the documentation but got nothing. – amid Jul 03 '15 at 15:23
-
a histogram is just a set of heights, right? so nothing a stem plot can't visualize. – Marcus Müller Jul 03 '15 at 17:05
As another answer pointed out the set_dashes method does not work for histograms. However, you can achieve fine control over the linestyle by passing a dash tuple directly to "linestyle" (see the documentation here https://matplotlib.org/3.1.0/gallery/lines_bars_and_markers/linestyles.html)
In your example, you could achieve the "loosely dashed" style in the link as follows
x=[7]*10
plt.hist(x, bins=5, range=(7.,11.), ec='k', facecolor='None',
linewidth=2, normed=1, linestyle=(0,(5,10)))
This works for me with matplotlib 3.0.3. Note I also, had to add ec='k'
to get the outline of the histogram to appear at all...

- 14,672
- 11
- 46
- 75

- 13
- 2