导入包

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. Maven提高篇系列之五——处理依赖冲突

    个人分类: Maven 不知道你在使用Maven时是否遇到过诸如"NoSuchMethodError"或"ClassNotFoundException"之类的问 ...

  2. 三.int , bool , str

     03.万恶之源-基本数据类型(int, bool, str) 本节主要内容: 1. python基本数据类型回顾 2. int----数字类型3. bool---布尔类型4. str--- 字符串类 ...

  3. 声笔飞码GB2312单字效率分析

    -----------------------声笔飞码强字方式单字效率分析-------------------------- 2   keys: 567       items, 381900209 ...

  4. Android学习之可滑动当前的Activity视图看见上一个活动的视图

    先看一下我的效果图吧: 好大的图啊!!! 百度音乐由一个很酷的功能,当前的UI可以滑动,然后看见上一个活动的UI,当时顿时觉得百度的牛人好多啊,能将如此前沿的技术应用到app上.当然如果你熟悉了And ...

  5. 注册Github账户过程

    1.首先打开冯老师提供的Github的教程网址:http://www.aehyok.com/Blog/Detail/73.html 2.打开Github的网站(https://github.com/) ...

  6. docker - 从安装到部署一个web应用(go、java)

    一:安装docker 1.https://docs.docker.com/engine/installation/binaries/ 下载docker最新版二进制tar.gz linux下: wget ...

  7. .net core部署到linux可能碰到的问题

    缺少icu库以独立部署 (SCD)的方式发包,运行时报错错误信息:FailFast: Couldn't find a valid ICU package installed on the system ...

  8. 调试 .NET Framework 源代码、.DotNetCore源码

    调试 .NET Framework 源代码..DotNetCore源码 如何调试 .NET Framework 源代码 在 Visual Studio 调试器中指定符号 (.pdb) 和源文件 .NE ...

  9. ipad协议

    getLoginQRCode (获取登录二维码)CheckLoginQRCode(检测扫码状态)ManualAuth(扫码登录)ManualAuth(62数据登录)ManualAuth(账号密码登录) ...

  10. C语言判断进程是否存在

    #include <windows.h> #include <tlhelp32.h> //进程快照函数头文件 #include <stdio.h> bool get ...