0
我有一个熊猫数据框,并且我试图绘制两列彼此。但问题是,我的xtick标签正在全面精确显示。如何缩写(我的Xaxis值是元组)?ax.xaxis.set_major_formatter不会更改Xtick标签显示数字的数量
fig = plt.figure(figsize=(13,160))
plt.style.use('ggplot')
sns.set_style('ticks')
plt.rcParams['font.size'] = 12
n = 0
for name, group in a.groupby(['Config','prot_state','repeat','leg']):
ax = fig.add_subplot(20,2,n+1)
group = group.sort_values(by='Lambda')
title = name[0]+'-'+name[1]+'-'+name[2]+'('+name[3]+')'
ax = group.plot(y='A', x='Lambda', ax=ax, marker='o', lw=3, label='Chain_A', title=title)
ax = group.plot(y='B', x='Lambda', ax=ax, marker='o', lw=3, label='Chain_B', title=title)
ax.set_xlabel('$\lambda_{Coul}$, $\lambda_{Vdw}$')
ax.set_ylabel('$\Delta G$ (KJ/mol)')
ax.get_xaxis().get_major_formatter()
sns.despine(offset=10, ax=ax)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=90, fontsize=10)
ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))
plt.xticks(range(len(group['Lambda'])), group['Lambda'])
n = n+1
fig = ax.get_figure()
plt.tight_layout()
fig.savefig('{}.pdf'.format('dg-dlambda'))
有谁知道我该如何缩写为两位数?
labels = ax.get_xticklabels()
new_labels = []
n_digits = 2 + 1 # you will use this for slicing the string, so the +1 is there so that your last digit doesn't get excluded. The first number (2) is arbitrary, depending how many you want.
for i in labels:
new_i = str(i)[:n_digits]
new_labels.append(new_i)
plt.xticks(labels, new_labels)
但是,这会:
显示的所有'''都是''',这很难重现你的代码,记得总是建议提供一个[Minimal,Complete,and Verifiable example](https://stackoverflow.com/help/mcve) )的问题,并编辑你的问题 关于这个问题本身,只是调用'ax.xaxis.set_major_formatter(FormatStrFormatter('%2f'))'应该做的伎俩 –
可能重复的[Matplotlib:指定格式的浮点数标记](https://stackoverflow.com/questions/29188757/matplotlib-specify-format-of-floats-for-tick-lables) –
我添加了ax.xaxis.set_major_formatter(FormatStrFormatter('%。1f '))到我的代码(见上面),但它实际上没有做任何事情 – mahzadkh