简体   繁体   English

旋转轴标签放置不正确(matplotlib)

[英]Rotated axis labels are placed incorrectly (matplotlib)

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. 我试图看一下Matplotlib Python Barplot:xtick标签的位置在彼此之间有不规则的间隔 ,但是由于它建立在条形图的布局上,因此我无法使其工作。 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);

在此处输入图片说明

By default matplotlib centers the tick labels at the tick positions. 默认情况下,matplotlib将刻度标签居中于刻度位置。 For 45° labels this can be confusing. 对于45°标签,这可能会造成混淆。 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. 我不确定结果是否更好/更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM