[英]How do I remove negative X axis labels in a population pyramid?
我有一個人口金字塔,但一側的 x 軸為負,有沒有辦法只重命名負 x 軸,使其為正?
# Draw Plot
plt.figure(figsize=(13,10), dpi= 80)
group_col = 't1_gender'
order_of_bars = df.agegroup.unique()[::-1]
colors = [plt.cm.Spectral(i/float(len(df[group_col].unique())-1)) for i in range(len(df[group_col].unique()))]
for c, group in zip(colors, df[group_col].unique()):
sns.barplot(x='count', y='agegroup', data=df.loc[df[group_col]==group, :], order=order_of_bars, color=c, label=group)
# Decorations
plt.xlabel("Number of calls")
plt.ylabel("Age group")
plt.yticks(fontsize=12)
plt.title("", fontsize=22)
plt.legend()
plt.savefig('images/figure2.png', dpi=300, facecolor=ax.get_facecolor(), transparent=True, pad_inches=0.0)
plt.show()
一個可能的解決方案是使用get_xticks()
獲取當前刻度,然后使用np.abs
函數強制刻度標簽為正:
xticks = plt.gca().get_xticks().astype(np.int)
plt.xticks(xticks, labels=np.abs(xticks));
(將代碼添加到#decoration 部分)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.