python 使用 matplotlib.pyplot来画柱状图和饼图
2018-11-16 21:31
573 查看
导入包
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()
结果
堆叠柱状图
# 显示高度 def autolabel(rects1, rects2): i = 0 for rect1 in rects1: rect2 = rects2 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()
结果
[i]并列柱状图
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 += width plt.bar(x, num_list2, width=width, label='2', tick_label=name_list, fc='g') plt.legend() plt.show()
结果
饼图
[i]最简饼图
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()
结果
带切割的饼图
# 圆形 plt.figure(1, figsize=(6, 6)) name_list = ['A', 'B', 'C', 'D', 'E'] num_list = [33, 44, 53, 30, 60] expl = [0, 0, 0, 0, 1] colors = ['pink', 'blue', 'yellow', 'green', 'red'] plt.pie(x=num_list,explode=expl, labels=name_list, autopct='%3.1f %%', colors=colors, pctdistance=0.3, shadow=True) plt.show()
结果
相关文章推荐
- 使用python的matplotlib(pyplot)画折线图和柱状图
- python中matplotlib.pyplot使用简介
- Python篇(二):matplotlib中pyplot的使用简介
- python: 使用matplotlib的pyplot绘制图表
- Python——使用matplotlib绘制柱状图
- Python绘图问题:Matplotlib中柱状图bar使用
- Python中使用matPlotlib绘图-曲线图、柱状图、散列点
- python使用matplotlib绘制柱状图教程
- Python:matplotlib中pyplot的使用方法
- Python中matplotlib pyplot常用的一些使用方法[Python 3.x]
- Python画图工具matplotlib的使用(图形并茂)
- 使用 python matplotlib 画矩形
- Python使用matplotlib实现在坐标系中画一个矩形的方法
- python使用matplotlib绘图 -- barChart
- Python绘图和数值工具:matplotlib 和 numpy下载与使用
- 使用 python Matplotlib 库绘图 .
- python实战二:使用CSV数据绘制带数据标志的折线图(matplotlib)
- Python Matplotlib的安装使用及Scipy,numpy,dateutil,pyparsing的安装
- python matplotlib绘图使用中文字体
- 在python中使用图形库matplotlib