首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180505A0VXMW00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券