如何将matplotlib 柱状图由垂直变成水平,水平翻转,再将y轴移到右边

由垂直变成水平:

由
ax.bar()

变成
ax.barh()

水平翻转:

#加上一条
plt.gca().invert_xaxis()

#同理,plt.gca().invert_yaxis() 是上下翻转

#同时别忘了把之前
ax.set_ylabel('Group')
ax.set_xlabel('Weights')
ax.set_title('Scores by group and gender')
ax.set_yticks(index)
ax.set_yticklabels(('N', 'I', 'E', 'G', 'B', 'C', 'J', 'H'))
#中的x,y颠倒回来

全部代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from collections import namedtuple

n_groups = 8

means_women = (0.296, 0.165, 0.157, 0.143, 0.104, 0.049, 0.024, 0.022)
#std_women = (3, 5, 2, 3, 3)

fig, ax = plt.subplots()

index = np.arange(n_groups)
bar_width = 0.5

opacity = 1
error_config = {'ecolor': '0.3'}

rects2 = ax.barh(index + bar_width, means_women, bar_width,
                alpha=opacity, color='#A2242F',
                label='Women')

#ax.set_ylabel('Group')
ax.set_xlabel('Women')
#ax.set_title('Scores by group and gender')
ax.set_yticks(index)
ax.set_yticklabels(('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'))
plt.gca().invert_xaxis()
ax.legend()

fig.tight_layout()
plt.show()

图变成了:

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值