我有一个条形图,x轴是(1,2,3 ... 12)。
所以我的条形图是这样的:
如何更改:
1---> -6month
2---> -1 year
3--->-1.5 year
.
.
.
正在显示吗?
我要绘制的代码是:
dffinal = df[['6month','final-formula','Question Text','numPatients6month']].drop_duplicates().sort_values(['6month'])
df = dffinal.drop('numPatients6month', 1).groupby(['6month','Question Text']).sum().unstack('Question Text')
df.columns = df.columns.droplevel()
ax=df.plot(kind='bar', stacked=True)
import matplotlib.pyplot as plt
ax2 = ax.twinx()
plt.xticks(fontsize=8, rotation=45)
#ax2.spines['right'].set_position(('axes', 1.0))
dffinal.plot(ax=ax2,x='6month', y='numPatients6month',visible=False)
plt.title('Cognitive Impairement-Stack bar')
plt.show()
我有两个df,因为我有两个y轴。
我尝试使用replace:
dffinal['6month'].replace(1, '-6 month',inplace=True)
dffinal['6month'].replace(2, '-1 year',inplace=True)
但是它没有用。
谢谢:)
答案 0 :(得分:1)
命令plt.xticks
应该照顾好它。根据x轴的计数是从0(默认值)还是从1(如您的图所示)开始,您可以尝试:
# If x starts from 0
plt.xticks(range(12), ['-6month','-1 year',...], fontsize=8, rotation=90)
或
# If x starts from 1
plt.xticks(range(1,13), ['-6month','-1 year',...], fontsize=8, rotation=90)
在两种情况下,都用所需标签的12个元素列表替换['-6month','-1 year',...]
。