python画柱状图-python 使用 matplotlib.pyplot来画柱状图和饼图

导入包

import matplotlib.pyplot as plt

柱状图

最简柱状图

# 显示高度

def autolabel(rects):

for rect in rects:

height = rect.get_height()

plt.text(rect.get_x()+rect.get_width()/2.- 0.2, 1.03*height, '%s' % int(height))

name_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']

num_list = [33, 44, 53, 16, 11, 17, 17, 10]

autolabel(plt.bar(range(len(num_list)), num_list, color='rgb', tick_label=name_list))

plt.show()

结果

1503464-20181116211927969-547665684.png

堆叠柱状图

# 显示高度

def autolabel(rects1, rects2):

i = 0

for rect1 in rects1:

rect2 = rects2[i]

i += 1

height = rect1.get_height() + rect2.get_height()

plt.text(rect1.get_x()+rect1.get_width()/2. - 0.1, 1.03*height, '%s' % int(height))

name_list = ['A', 'B', 'C', 'D']

num_list = [10, 15, 16, 28]

num_list2 = [10, 12, 18, 26]

z1 = plt.bar(range(len(num_list)), num_list, label='1', fc='b')

z2 = plt.bar(range(len(num_list)), num_list2, bottom=num_list, label='2', tick_label=name_list, fc='g')

autolabel(z1, z2)

plt.legend()

plt.show()

结果

1503464-20181116212220157-1037303371.png

并列柱状图

name_list = ['A', 'B', 'C', 'D']

num_list = [10, 15, 16, 28]

num_list2 = [10, 12, 18, 26]

x = list(range(len(num_list)))

total_width, n = 0.8, 2

width = total_width / n

plt.bar(x, num_list, width=width, label='1', fc='b')

for i in range(len(x)):

x[i] += width

plt.bar(x, num_list2, width=width, label='2', tick_label=name_list, fc='g')

plt.legend()

plt.show()

结果

1503464-20181116212345166-263476573.png

饼图

最简饼图

name_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']

num_list = [33, 44, 53, 6,11, 7, 7, 10, 3, 1]

# 保证圆形

plt.axes(aspect=1)

plt.pie(x=num_list, labels=name_list, autopct='%3.1f %%')

plt.show()

结果

1503464-20181116212543616-2090436686.png

带切割的饼图

name_list = ['A', 'B', 'C', 'D']

num_list = [10, 3, 3, 47]

colors = ['green', 'yellow', 'blue', 'red']

# 圆形

plt.figure(1, figsize=(6, 6))

#决定分割部分,及其与其它部分之间的间距

expl = [0, 0, 0, 0.1]

plt.pie(x=num_list, explode=expl, labels=name_list, autopct='%3.1f %%', colors=colors, shadow=True)

plt.show()

结果

1503464-20181226083031329-1853757663.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值