gpt4 book ai didi

python - 限制 xticks pandas 条形图中的字符

转载 作者:行者123 更新时间:2023-12-05 04:06:43 31 4
gpt4 key购买 nike

我有以下代码:

    df = input_df_.loc[input_df_.index.isin(inds)]
df = df.drop(expected_metrics+metrics+['AgeNBR'],axis=1).mean()\
.sort_values(ascending=False)[:25]
df.plot(kind='bar',width=.4,position=1,color='red')
full_df = input_df_[df.index].mean()
full_df.plot(kind='bar',width=.4,position=0,color='blue')

plt.show()

difference = full_df - df

difference.plot(kind='bar',color = ['r' if x > 0 else 'b' for x in difference.values])
plt.show()

代码有效,但由于某些 x-ticks 非常长,因此两张图像最终相距甚远。有没有办法限制单个 x-ticks 中文本的大小?类似于前 7 个字符?

最佳答案

下面是提问者是如何解决这个问题的:

    plt.subplot(1,2,1)
df.plot(kind='bar',width=.4,position=1,color='red')
full_df = input_df_[df.index].mean()
full_df.plot(kind='bar',width=.4,position=0,color='blue')\
.set_xticklabels([str(tick)[:45] for tick in full_df.index])
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.gca().set_title('Selected and full feature set averages',fontsize=30)

plt.subplot(1,2,2)
difference = df - full_df

difference.plot(kind='bar',color = ['r' if x > 0 else 'b' for x in difference.values]).\
set_xticklabels([str(tick)[:45] for tick in difference.index])
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.gca().set_title('Selected minus full feature set averages', fontsize=30)
plt.show()

关于python - 限制 xticks pandas 条形图中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49525479/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com