【问题标题】:Xaxis text overlapping - MatplotlibXaxis 文本重叠 - Matplotlib
【发布时间】:2014-12-26 11:14:39
【问题描述】:

我使用 ma​​tplotlib 绘制了一个条形图,一切正常。当X轴上的“文本标签”对于每个数据点来说太长时,文本重叠,如图所示。我还尝试将对齐方式旋转到 4590 度,但文本超出了框架。

X = [1,2,3,4,5,6]
Y = [55,46,46,36,27,9]
point_labels =[50,38,25,6,6,6]
fig, axes = plt.subplots(1)
ax2 = axes
n=7
ind=np.arange(n)
width=0.1
axis_font = {'fontname':'Arial', 'size':'11'}
Color = ['0.1','0.2','0.3','0.4','0.5','0.6']

bar = ax2.bar(X,Y,align="center",width=0.5,color=Color)
bar[0].set_color('r')

plt.xticks(ind+width/2, ('','Insufficient skills','Complex hardware and software','High initial investments','Complexity in collection and storing data','Insufficient hardware power for computing','Data collaboration'),horizontalalignment='center')

plt.ylabel('in precentage (%)')

text(4.6,52, '100% = 11 responses', style='italic',bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})

axes.yaxis.grid()
axes.set_ylim([0,60])

plt.show()

任何人都可以提出解决此问题的想法吗?

【问题讨论】:

  • 你不能添加一个图例来代替轴标签吗?
  • @giosans 如果我找不到解决方案,那将是我的最后一种方法。但我相信肯定应该有一个解决方法!

标签: python python-2.7 python-3.x matplotlib


【解决方案1】:

我能想到的最好的方法是将旋转设置为垂直更改图形扩展并运行plt.tight_layout()。 这些将是您代码的最后 3 行:

mngr = plt.get_current_fig_manager()
mngr.window.setGeometry(50,50,960, 640)
plt.tight_layout()

或者,您应该使用带有\n 的多行。 见http://matplotlib.org/examples/pylab_examples/multiline.html

【讨论】:

    【解决方案2】:

    以下解决方案对我有用

    How to prevent overlapping x-axis labels in sns.countplot

    plt.xticks(
        rotation=45, 
        horizontalalignment='right',
        fontweight='light',
        fontsize='x-large'  
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      相关资源
      最近更新 更多