检查下图的x轴.如何将标签向左移动一点,使它们与各自的刻度对齐?
我正在使用以下方式旋转标签:
ax.set_xticks(xlabels_positions)
ax.set_xticklabels(xlabels, rotation=45)
Run Code Online (Sandbox Code Playgroud)
但是,正如您所看到的,旋转以文本标签的中间为中心.这使它看起来像是向右移动了.
我试过用这个代替:
ax.set_xticklabels(xlabels, rotation=45, rotation_mode="anchor")
Run Code Online (Sandbox Code Playgroud)
......但它没有按照我的意愿行事.并且"anchor"似乎是rotation_mode参数允许的唯一值.

如何在matplotlib中旋转xticklabels以使每个xticklabel之间的间距相等?
例如,使用此代码:
import matplotlib.pyplot as plt
import numpy as np
# Data + parameters
fontsize = 20
t = np.arange(0.0, 6.0, 1)
xticklabels = ['Full', 'token emb', 'char emb', 'char LSTM',
'token LSTM', 'feed forward','ANN']
# Plotting
fig = plt.figure(1)
ax = fig.add_subplot(111)
plt.plot(t, t)
plt.xticks(range(0, len(t) + 1))
ax.tick_params(axis='both', which='major', labelsize=fontsize)
ax.set_xticklabels(xticklabels, rotation = 45)
fig.savefig('test_rotation.png', dpi=300, format='png', bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)
我获得:
每个xticklabel之间的间距是不相等的.例如,"Full"和"token emb"之间的间距远大于"前馈"和"ANN"之间的间距.
我在Windows 7 SP1 x64 Ultimate上使用Matplotlib 2.0.0和Python 3.5 64位.
这是我的数据:
a3=pd.DataFrame({'OfficeName':['a','b','c','d','e','f','g','h'],
'Ratio': [0.1,0.15,0.2,0.3,0.2,0.25,0.1,0.4]})
Run Code Online (Sandbox Code Playgroud)
这是我绘制条形图的代码:
fig, ax = plt.subplots()
ind = np.arange(a3.loc[:,'OfficeName'].nunique()) # the x locations for the groups
width = 0.35 # the width of the bars
p1 = ax.bar(ind,a3.loc[:,'Ratio'],width)
ax.set_title('Ratio of refunds to purchases')
ax.set_xticklabels(a3.loc[:,'OfficeName'],ha='center')
#ax.set_xticklabels(['a','b','c','d','e','f','g','h'],ha='center')
ax.set_xlabel('x Group')
ax.set_ylabel('Ratio')
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是,在我的图表中,第一个 x 标签丢失了:
我认为这个问题与Matplotlib不同:将x轴刻度标签向左移动一个位置,因为我什至不需要旋转x标签。
谁能解释为什么会发生这种情况以及如何解决它?