I want to build a histogram with bin data.
data
## x_da: 1,2,3,4,5 x_da = np.arange(1,1+5*1,1) ## bin setting bin_range = ["< 1 ","1 - 2","2 - 3","3 - 4","> 4"] ## Counts base on the bin(Already st) y_da = np.array([178,2301,2880,1686,1715])
Plot
fig = plt.figure(figsize= (5,3)) ax = plt.subplot(111) plt.bar(x_da,y_da,width=1,edgecolor='none',align='center', \ fill=True,facecolor = "green",zorder = 1 ,alpha=0.5) ax.get_yaxis().set_tick_params(which='both', direction='out') ax.get_xaxis().set_tick_params(which='both', direction='out')
Drawing
http://7xrn7f.com1.z0.glb.clouddn.com/16-3-9/18987922.jpg
My goal
- adjust the xtickslines, which are extensions of the vertical edges of the bar, as shown below:
http://7xrn7f.com1.z0.glb.clouddn.com/16-3-9/5475187.jpg
- xticklabels are still in the same position (middle of each bar)
My attempt
My method was to make the cross contain these positions above and set the invisible midpoints.
With code like this:
ax.set_xticks(np.arange(0.5,1+10*0.5,1)) for xtick in ax.xaxis.get_ticklines()[1::2]: xtick.set_visible(False) ax.set_xticklabels(bin_range,fontsize = 14)
http://7xrn7f.com1.z0.glb.clouddn.com/16-3-9/78024677.jpg
The "invisible" approach resolved the problem in a simple way, but the xticklabels position also changed.
I don't want to add some "" (empty line) to bin_range to jump over one xtickline. Can someone offer some recommendations to solve this problem in the cleanest way?