amid
amid

Reputation: 51

How can I set the dash length in pyplot.hist?

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)

Upvotes: 0

Views: 1190

Answers (2)

astrogenerozov
astrogenerozov

Reputation: 13

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...

Upvotes: 1

Marcus Müller
Marcus Müller

Reputation: 36352

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

  1. use numpy's histogram function; it's used by pyplot's hist, anyway, and then
  2. plot the results using stem.

Upvotes: 1

Related Questions