导入包

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

结果

并列柱状图

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

结果

饼图

最简饼图

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

结果

带切割的饼图

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

结果

python 使用 matplotlib.pyplot来画柱状图和饼图的更多相关文章

  1. Python中matplotlib.pyplot.imshow画灰度图的多种方法

    转载:https://www.jianshu.com/p/8f96318a153f matplotlib库的教程和使用方法此处就不累赘了,网上有十分多优秀的教程资源.此处直接上代码: def demo ...

  2. Python:matplotlib.pyplot

    翻译总结自:matplotlib.pyplot - Matplotlib 3.4.3 documentation 函数 说明 acorr x的自相关性图 angle_spectrum 角度谱 anno ...

  3. python的matplotlib.pyplot绘制甘特图

    博主本来就想简单地找一下代码,画一幅甘特图,结果百度之后发现甘特图的代码基本都不是用matplotlib库,但是像柱状图等统计图通常都是用这个库进行绘制的,所以博主就花了一些时间,自己敲了一份代码,简 ...

  4. python中matplotlib.pyplot中cm的属性

    https://matplotlib.org/gallery/color/colormap_reference.html

  5. Python:matplotlib.cm 色表

    官网:Choosing Colormaps in Matplotlib - Matplotlib 3.5.0 documentation Colormap与matplotlib.cm 我们以等高区域函 ...

  6. Python:Matplotlib 画曲线和柱状图(Code)

    原文链接:http://blog.csdn.net/ikerpeng/article/details/20523679 参考资料:http://matplotlib.org/gallery.html ...

  7. python画柱状图并且输出到html文件

    import matplotlibmatplotlib.use('Agg')import matplotlib.pyplot as pltfrom Cstring import StringIO y ...

  8. Python 的 Matplotlib 画图库

    Matplotlib安装 NumPy库方便数值运算,但枯燥的数据并不利于人们的直观理解. 数据需要可视化. Matplotlib:一个数据可视化函数库 使用前需要安装  利用Python自带 ...

  9. Python 中 plt 画柱状图和折线图

    1. 背景 Python在一些数据可视化的过程中需要使用 plt 函数画柱状图和折线图. 2. 导入 import matplotlib.pyplot as plt 3. 柱状图 array= np. ...

随机推荐

  1. poj3061

    #include<stdio.h> #include<iostream> using namespace std; #include<algorithm> cons ...

  2. execl 导出

    /** * 导出   是把数表中的数据添加到execl表中 */ public function export(){ $xlsData = Db('user')->select(); Vendo ...

  3. bootstrap 警告框单个删除

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  4. 电容器的ESR

    电容器的ESR(等效串联电阻)Equivalent Series Resistance    作为开关电源的输出整流滤波电容器,电容量往往是首要的选择,铝电解电容器的电容量完全可以满足要求,而ESR则 ...

  5. 1.mybatis入门

    一:创建表 CREATE TABLE `country` ( `id` ) NOT NULL AUTO_INCREMENT, `countryname` varchar() DEFAULT NULL, ...

  6. unigui在阿里云服务器上部署

    unigui在阿里云服务器上部署 客户租用了阿里云WINDOWS2008服务器,部署UNIGUI发现死活不行,WINDOWS2008自带的IE9浏览器打开URL,卡死在loading...... 我远 ...

  7. 基于Redis的CAS集群

    单点登录(SSO)是复杂应用系统的基本需求,Yale CAS是目前常用的开源解决方案.CAS认证中心,基于其特殊作用,自然会成为整个应用系统的核心,所有应用系统的认证工作,都将请求到CAS来完成.因此 ...

  8. RocketMQ概述

    概述 ApacheRocketMQ是一个低延时.高性能.可靠.海量并且灵活扩展性的分布式消息和流平台,于2017年9月25日成为Apache基金会顶级开源项目.它由4个部分组成:name server ...

  9. JS学习笔记4_BOM

    1.frame相关对象 top:指向最外层框架,使用top可以在一个框架中访问另一个框架,例如top.frames[index/name] parent:指向当前框架的直接上层框架 window:代码 ...

  10. OSLab多进程

    日期:2019/3/23 内容:Linux下与多进程相关的函数.     进程基本知识 定义 应用程序关于某数据集合上的一次运行活动. 特点 ·操作系统进行资源分配和调度的基本单位 ·进程是程序的一次 ...