The cleanest way to install xtickslabel in a specific position

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') ## Set the "range" mapping to its bar ax.set_xticks(np.arange(1,1+5*1,1)) ax.set_xticklabels(bin_range,fontsize = 14) 

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

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

+7
python numpy matplotlib histogram
source share
1 answer

The easiest solution I have found is to use basic symbols to indicate labels where bars start / end but just leave their labels empty. You can then set the low tick positions to be in the middle of the bars and set their labels as the labels you created. Thus, you control two completely different sets of ticks (primary and secondary) and you can customize their display.

Here is the code I could come up with.

 import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import FixedLocator, FixedFormatter barwidth = 1 x_da = np.arange(0, 5 * barwidth, barwidth) y_da = np.array([178,2301,2880,1686,1715]) bin_labels = ["< 1 ","1 - 2","2 - 3","3 - 4","> 4"] fig = plt.figure(figsize=(5,3)) ax = plt.subplot(111) plt.bar(x_da,y_da,width=barwidth, 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') # Creat major ticks at the bars - 0.5 * barwidth plus one at the end major_locations = np.append(x_da, x_da[-1] + barwidth) - (0.5 * barwidth) ax.set_xticks(major_locations) # Don't display *any* major ticks labels ax.set_xticklabels('') # Create minor ticks and labels # Display the labels at MINOR ticks spaced between the bars ax.xaxis.set_minor_locator(FixedLocator(x_da)) ax.xaxis.set_minor_formatter(FixedFormatter(bin_labels)) # Now actually hide the minor ticks but leave the labels ax.tick_params(axis='x', which='minor', length=0, labelsize=14) 

enter image description here

+2
source share

All Articles