matplotlib barplot, generic way of setting the xticklabels in the middle of the bar

前端 未结 1 1868
天涯浪人
天涯浪人 2020-12-21 07:31

Following code generates a barplot with the xticklabels centered for each bar. However, scaling the x-axis, changing the number of bars or changing the bar width does alter

相关标签:
1条回答
  • 2020-12-21 08:23

    So the problem is that you only call ax.set_xticklabels. This fixes the labels, but the tick positions are still handled by the AutoLocator, which will add/remove ticks upon changing the axis limits.

    So you also need to fix the tick positions:

    ax.set_xticks(x)
    ax.set_xticklabels(xl)
    

    By calling set_xticks the AutoLocator is replaced under the hood with a FixedLocator.

    And then you can center the bars to make it look nicer (optional):

    ax.bar(x, y, 0.5, align='center')
    
    0 讨论(0)
提交回复
热议问题