python 条形图 stack_python用matplotlib画条形图初探‘单变量,双变量堆叠

一、包

python]view plain copy

import numpy as np

import matplotlib.pyplot as plt

二、单变量(垂直)

[python]view plain copy

y = [5, 10, 15, 20, 25]

index = np.arange(len(y))

plt.bar(left=index, height=y, color='r', width=0.5) # 水平对应left&height, weidth表示bar的宽度

# plt.xlim(index.min(), index.max()+1) 设定x轴的范围

# plt.ylim(min(y), max(y)+5)

plt.xlabel('index')

plt.ylabel('y')

plt.title('barplot')

plt.show()

三、单变量(水平)

[python]view plain copy

plt.bar(left=0, bottom=index, width=y, color='red', height=0.5, orientation='horizontal') # 水平对应bottom&width, height表示bar的宽度

plt.ylabel('index')

plt.xlabel('y')

plt.title('barplot horizontal')

plt.show()

# barh可以省略orientation参数的设定

plt.barh(left=0, bottom=np.arange(len(y)), width=y, color='red', height=0.5)

四、双变量(dodge)

[python]view plain copy

x = [52, 69, 58, 12, 39, 75]

y = [56, 15, 84, 65, 45, 48]

index = np.arange(len(x))

width = 0.3

plt.bar(left=index, height=x, width=width, color='yellow', label=u'x')

plt.bar(left=index+width, height=y, width=width, color='red', label=u'y') # width=index+width 表示从向右x平移width的单位,刚好靠在一起

plt.xlabel('index')

plt.ylabel('x/y')

plt.title('barplot dodge')

plt.legend(loc='best')

plt.show()

我知道很多人都在为自学而感到苦恼,没有人你能够和你一起交流互相分享经验,遇到问题而没办法解决,久而久之你就会选择放弃,然而花费的时间却没有回来。还有我知道绝大多数的人没有一个良好的学习环境,以及系统的学习资料,在网上找的那些资料杂乱无章学习起来毫无头绪。 不过这些问题在我这里都不是问题。加上我的qun,里面有很多的大牛,每天晚上还会邀请名师过来讲座。并且有一个良好的学习环境。

五、双变量(stack)

[python]view plain copy

plt.bar(left=index, height=x, width=width, color='yellow', label=u'x')

plt.bar(left=index, height=y, width=width, color='red', bottom=x, label=u'y') # bottom=x,表示从x的值开始堆叠上去

plt.xlabel('index')

plt.ylabel('x/y')

plt.title('barplot stack')

plt.legend(loc='best')

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值