简体   繁体   中英

Round bars in matplotlib histogram

I use matplotlib.pyplot.hist() for a histogram plot in python. If I use a visible edgewidth, for example when using histtype='step' , the upper corners of the single bars are slightly round. I want them to be sharply rectangular instead. I already tried using the solid_capstyle keyword, which works for influencing the shape of line plots, but this doesn't work in hist() . Any ideas on how to do that? Thanks!

Here's my minimal self-contained example

import numpy as np
import matplotlib.pyplot as plt

mu = 200
sigma = 25
x = mu + sigma*np.random.randn(10000)

fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(8, 4))
ax0.hist(x, 20, normed=1, histtype='step', facecolor='g', alpha=0.75, linewidth=4.)
ax0.set_title('step')
# Create a histogram by providing the bin edges (unequally spaced).
bins = [100, 150, 180, 195, 205, 220, 250, 300]
ax1.hist(x, bins, normed=1, histtype='step', rwidth=0.8, linewidth=4.)
ax1.set_title('unequal bins')
plt.tight_layout()
plt.savefig('test.png', dpi=200)

The capability to control this property is available in MatPlotLib 1.4. So, I recommend upgrading; for example if you use pip:

pip install --upgrade matplotlib

Then use the joinstyle keyword argument ( ['miter' | 'round' | 'bevel'] ) in your calls to hist ; eg:

ax0.hist(x, 20, normed=1, histtype='step',
         facecolor='g', alpha=0.75, linewidth=4.,
         joinstyle='miter')

Note that 'miter' (square corners) appears to be the default in MPL 1.4, so specifying this parameter is not actually necessary in your case. I've included it here to be explicit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM