简体   繁体   中英

How do I remove negative X axis labels in a population pyramid?

I have a population pyramid but the x axis on one side is negative, is there a way to rename just the negative x axis so that it is positive?

# 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()

嘛

A possible solution is to get the current ticks using get_xticks() and then use the np.abs function to force the tick labels to be positive:

xticks = plt.gca().get_xticks().astype(np.int)
plt.xticks(xticks, labels=np.abs(xticks));

(add the code to the #decoration section)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM