【问题标题】:Cleanest way to set xtickslabel in specific position在特定位置设置 xticks 标签的最干净方法
【发布时间】:2016-06-24 02:02:07
【问题描述】:

我想用分箱数据绘制直方图。

数据

## 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])      

情节

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)    

http://7xrn7f.com1.z0.glb.clouddn.com/16-3-9/18987922.jpg

我的目标

  1. 调整代表条形垂直边缘延伸的 xtickslines 的位置,如下所示:

http://7xrn7f.com1.z0.glb.clouddn.com/16-3-9/5475187.jpg

  1. xticklabels 仍然位于相同的位置(每个条的中点)

我的尝试

我的方法是创建一个包含上述位置的 xticks,并将中点设置为不可见。
使用这样的代码:

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

“隐形”方法以一种简单的方式解决了这个问题,但 xticklabels 的位置也发生了变化。

我不想在 bin_range 中添加一些“”(空字符串)以跳过一个 xtickline。有人可以提供一些建议以最干净的方式解决这个问题吗?

【问题讨论】:

    标签: python numpy matplotlib histogram


    【解决方案1】:

    我找到的最简单的解决方案是使用主要 xticks 来表示条形开始/结束的刻度线,但只需将它们的标签留空。然后,您可以将次要刻度位置设置在条形的中间,并将它们的标签设置为您创建的标签。这样您就可以管理两组完全不同的刻度(主要和次要),并且可以独立调整它们的显示。

    这是我想出的代码。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 2017-09-28
      • 2023-03-07
      • 2010-09-06
      相关资源
      最近更新 更多