2016-03-27 118 views
0

我有以下情节:旋转Matplotlib刻度标记导致奇怪的间隔发出

enter image description here

我想使x轴蜱通过〜40度旋转蜱更具有可读性。所以从:

plt.xticks(list(range(0, width)), list(df_100.columns), rotation='90', fontsize=16)

要:

plt.xticks(list(range(0, width)), list(df_100.columns), rotation='40', fontsize=16)

当我这样做,不过,我得到一些疯狂的间距问题:

enter image description here

(忽略的变化在颜色...)

导致此问题的原因是什么?我该如何解决它?下面是一个最小工作示例:

import matplotlib.pyplot as plt 
import numpy as np 

# Z is your data set 
N = 100 
height = df_100.shape[0] 
width = df_100.shape[1] 
# Z = np.random.random((100, 29)) 

# G is a NxNx3 matrix 
G = np.zeros((height,width,3)) 

# Where we set the RGB for each pixel 
G[Z>0.5] = [1, 1, 1] 
G[Z<0.5] = [0.25, 0.25, 0.25] 

fig, ax = plt.subplots(figsize=(20, 10)) 
ax.imshow(G, interpolation='none') 

ax.set_aspect('auto') 
ax.grid(None) 
ax.xaxis.tick_top() 
plt.xticks(list(range(0, width)), list(df_100.columns), rotation='45', fontsize=16) 
plt.yticks([0, df_100.shape[0] - 1], [1, df_100.shape[0]], fontsize=20) 
plt.tight_layout() 
plt.show() 
+0

图像显示不正确,似乎也没有颜色变化... –

回答

1

如果xticklabels的长度相同,则不会出现此类问题。但是,考虑到不同长度的标签,你可能会遇到这种问题。因为默认旋转来自xlabel字符串的中心。因此,您可以尝试从 ['right','center','left']正确设置旋转锚点。

ha = 'left' # or 'right'. Experiment with it. 
ax.set_xticks(x) # set tick location 
ax.set_xticklabels(xlabels, rotation=40, ha=ha) # rotate the labels with proper anchoring. 
+0

这工作---谢谢你!看起来“左”是正确的设置 - “正确”会导致更多问题。我猜测旋转方向是原因。 –