导入包

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. 《mysql必知必会》学习_第10章_20180731_欢

    第10章,计算字段. P64 select concat (vend_name,'(',vend_country,')') from vendors order by vend_name; # 拼接, ...

  2. 两个jsp文件运行后弹出对话框 下载文件问题

    这个问题是两个jsp字符编码不一致的问题 如图所示 划线部分是要特别注意的地方 出错往往是这里 有时是“;”后面有无空格 如果login.jsp有 那么loginCheck.jsp也必须要有而且是同样 ...

  3. Codeforces Beta Round #27 E. Number With The Given Amount Of Divisors 含n个约数最小数

    http://codeforces.com/problemset/problem/27/E RT,求含n个约数的最小的数 我们设答案p = 2^t1 * 3^t2 * -- * p^tk(其中p是第k ...

  4. hdu 5073 有坑+方差贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=5073 就是给你 n 个数,代表n个星球的位置,每一个星球的重量都为 1 开始的时候每一个星球都绕着质心转动,那么 ...

  5. 二分图学习——基础dfs判断二分图

    #include <iostream> #include <cstdio> #include <string.h> #include <vector> ...

  6. kmp小记

    以下转载自Matrix67 ************************************************************************************** ...

  7. [leed code 179] Largest Number

    1 题目 Given a list of non negative integers, arrange them such that they form the largest number. For ...

  8. ADO.NET系列之事务和调用存储过程

    ADO.NET系列之Connection对象 ADO.NET系列之Command对象 ADO.NET系列之DataAdapter对象 ADO.NET系列之事务和调用存储过程 前几篇我们介绍了Conne ...

  9. WinForm中实现Loading加载界面

    1,LoaderForm窗体中添加PictureBox,然后添加Loading图片 2,窗体内属性设置 StartPosition :CenterScreen在屏幕中心显示 TopMost:True置 ...

  10. C# 动态创建SQL数据库(二)

    使用Entity Framework  创建数据库与表 前面文章有说到使用SQL语句动态创建数据库与数据表,这次直接使用Entriy Framwork 的ORM对象关系映射来创建数据库与表 一 新建项 ...