旋转轴标签放置不正确(matplotlib) [英] Rotated axis labels are placed incorrectly (matplotlib)
问题描述
我想绘制带有旋转标签的相关矩阵.但是,标签错位,如下所示.我试过查看 Matplotlib Python Barplot:xtick标签的位置在彼此之间有不规则的空间,但是由于它建立在条形图的布局上,因此我无法使其在我的情况下起作用.使用以下代码添加标签:
I want to plot a correlation matrix with rotated labels. However, the labels are misplaced as seen below. I've tried to look at Matplotlib Python Barplot: Position of xtick labels have irregular spaces between eachother, but I can't make it work in my case as it builds on the layout of the bar diagram. The labels are added using the following code:
fig = plt.figure()
ax1 = fig.add_subplot(111)
varLabels = ['n_contacts', 'n_calls', 'n_texts', 'dur_calls', 'morning', 'work-hours', 'evening', 'night', 'weekdays', 'friday', 'saturday', 'sunday']
ax1.set_xticks(np.arange(0,12))
ax1.set_yticks(np.arange(0,12))
ax1.set_xticklabels(varLabels, rotation=45);
ax1.set_yticklabels(varLabels, rotation=45);
推荐答案
默认情况下,matplotlib 将刻度标签居中放置在刻度位置.对于 45° 标签,这可能会令人困惑.但是,您可以编辑对齐属性以使右对齐(分别是顶部对齐).
By default matplotlib centers the tick labels at the tick positions. For 45° labels this can be confusing. However, you can edit the alignment properties to make the right aligned (and top aligned respectively).
...
ax1.tick_params(direction='inout')
ax1.set_xticklabels(varLabels, rotation=45, ha='right')
ax1.set_yticklabels(varLabels, rotation=45, va='top')
...
我不确定结果是否更好/更清晰.
I'm not sure if the result is better / clearer though.
这篇关于旋转轴标签放置不正确(matplotlib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!