python - Removing the bottom error caps only on matplotlib -
i wanted display half error bars, symetric ; had no clue how "clean way", chose use asymetric errors 0 on bottom side ; then, when displayed caps, realised not best way this.
here's code :
import numpy np import matplotlib.pyplot plt n = 5 men_means = (20, 35, 30, 35, 27) men_std = (2, 3, 4, 1, 2) ind = np.arange(n) width = 0.35 fig, ax = plt.subplots() rects1 = ax.bar(ind, men_means, width, color='r',yerr=[np.zeros(len(men_std)),men_std],capsize = 5) women_means = (25, 32, 34, 20, 25) women_std = (3, 5, 2, 3, 3) rects2 = ax.bar(ind + width, women_means, width, color='y',yerr=[np.zeros(len(women_std)),women_std],capsize = 5) plt.show()
and plot :. can see, way of plotting half error bars not should done.
so there way hide bottom cap line or better way plot half error bars ?
ax.errorbar
has option set uplims=true
or lolims=true
signify means repesent upper or lower limits, respectively. unfortunately, doesn't seem can use these options directly ax.bar
, have plot errorbar , bar plot separately.
the documentation uplims/lolims
options in ax.errorbar
:
lolims
/uplims
/xlolims
/xuplims
: bool, optional, default:nonethese arguments can used indicate value gives upper/lower limits. in case caret symbol used indicate this. lims-arguments may of same type
xerr
,yerr
. use limits inverted axes,set_xlim()
orset_ylim()
must called before errorbar().
note using option changes caps arrows. see below example of how change them caps, if need flat caps instead of arrows.
you can see these options in action in this example on matplotlib website.
now, here's example, modified:
import numpy np import matplotlib.pyplot plt n = 5 men_means = (20, 35, 30, 35, 27) men_std = (2, 3, 4, 1, 2) ind = np.arange(n) width = 0.35 fig, ax = plt.subplots() rects1 = ax.bar(ind, men_means, width, color='r') err1 = ax.errorbar(ind, men_means, yerr=men_std, lolims=true, capsize = 0, ls='none', color='k') women_means = (25, 32, 34, 20, 25) women_std = (3, 5, 2, 3, 3) rects2 = ax.bar(ind + width, women_means, width, color='y') err2 = ax.errorbar(ind + width, women_means, yerr=women_std, lolims=true, capsize = 0, ls='none', color='k') plt.show()
if don't arrows, change them flat caps, changing marker of caplines
returned (as second item) ax.errorbar
. can change them arrows marker style _
, , control size .set_markersize
:
import numpy np import matplotlib.pyplot plt n = 5 men_means = (20, 35, 30, 35, 27) men_std = (2, 3, 4, 1, 2) ind = np.arange(n) width = 0.35 fig, ax = plt.subplots() rects1 = ax.bar(ind, men_means, width, color='r') plotline1, caplines1, barlinecols1 = ax.errorbar( ind, men_means, yerr=men_std, lolims=true, capsize = 0, ls='none', color='k') caplines1[0].set_marker('_') caplines1[0].set_markersize(20) women_means = (25, 32, 34, 20, 25) women_std = (3, 5, 2, 3, 3) rects2 = ax.bar(ind + width, women_means, width, color='y') plotline2, caplines2, barlinecols2 = ax.errorbar( ind + width, women_means, yerr=women_std, lolims=true, capsize = 0, ls='none', color='k') caplines2[0].set_marker('_') caplines2[0].set_markersize(10) plt.show()
Comments
Post a Comment