目录

  • 1、ax.bar()函数
  • 2、绘制单个条形图
  • 3、绘制水平交错的条形图
  • 4、多个条形图堆叠的绘制
  • 5、条形图横向显示

1、ax.bar()函数

条形图可以垂直或水平绘制。条形图显示离散类别之间的比较。图表的一根轴显示被比较的具体类别,另一根轴代表测量值。Matplotlib中,条形图用以下的函数来画:

ax.bar(x, height, width, bottom, align)

该函数制作了一个大小为(x -width=2;x+width=2;底;底+高)的约束矩形的条形图。
参数解释如下:

参数 解释
x x是 控制条形图的中心(默认)还是左边缘
height 标量或标量序列代表条形图的高度
width 标量或类数组,可选。条形图的宽度默认为0.8
bottom 标量或类数组,可选。条形图的Y坐标,默认为None
align {‘center’, ‘edge’}, 可选,默认’center’
tick_label 条形图的刻度标签。默认值。无 (使用默认的数字标签。)
更多参数 点击官方文档

2、绘制单个条形图

以下是Matplotlib条形图的一个简单例子。它显示了一个机构提供的各种课程的注册学生人数:

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False    # 用来正常显示负号
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
langs = ['数学', '英语', '物理', '化学', '生物']
students = [30,19,45,30,25]
ax.bar(langs,students)
plt.show()

显示结果如下:

3、绘制水平交错的条形图

有时候,我们还要给条形图增加图例,加入一些对比,并且还要给对比的使用不同的颜色,代码示例如下:

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False    # 用来正常显示负号
data = [[30, 25, 50, 20],
[40, 23, 51, 17],
[35, 22, 45, 19]]
# x轴上坐标的个数,也是data列表里面的列表内的元素个数。4也可以用len(data[0])代替。
X = np.arange(4)
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
'''
F1、F2、F3画柱状图,共三个,第一个参数可以认为是labels,第二个参数是各自的y坐标,如果我们想要
在某个柱状图下面设置x轴刻度值,就可以在该柱状图下面用tick_label设置。
'''
F1 = ax.bar(X + 0.00, data[0], color = 'b', width = 0.25)
F2 = ax.bar(X + 0.25, data[1], color = 'g', width = 0.25,tick_label=[2017,2018,2019,2020])
F3 = ax.bar(X + 0.50, data[2], color = 'r', width = 0.25)
# 给柱状图设置图例,第一个参数是个元组,元组中每个元素代表一个柱状图,第二个参数是对应着的图例的名字。
ax.legend((F1,F2,F3),('数学','英语','物理'))
plt.show()

显示结果如下:

上述代码注释有点儿多,请认真阅读。

4、多个条形图堆叠的绘制

代码与3中的大部分都一样,唯一改动的就是ax.bar中的第一个参数,讲水平位移的量给删除,就默认得到了数值方向上的堆叠,代码如下:

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False    # 用来正常显示负号
data = [[30, 25, 50, 20],
[40, 23, 51, 17],
[35, 22, 45, 19]]
X = np.arange(len(data[0]))
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
# 唯一改动的就是ax.bar中的第一个参数。
F1 = ax.bar(X, data[0], color = 'b', width = 0.25)
F2 = ax.bar(X, data[1], color = 'g', width = 0.25,tick_label=[2017,2018,2019,2020])
F3 = ax.bar(X, data[2], color = 'r', width = 0.25)
ax.legend((F1,F2,F3),('数学','英语','物理'))
plt.show()

结果如下:

此外,还可以给柱状图添加一个误差条,如下代码:

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False    # 用来正常显示负号
data = [[30, 25, 50, 20],
[40, 23, 51, 17],
[35, 22, 45, 19]]
F1_std = [2,3,4,1]
F2_std = [3,1,2,1]
F3_std = [3,1,2,1]
X = np.arange(len(data[0]))
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
# 唯一改动的就是ax.bar中的第一个参数。
F1 = ax.bar(X, data[0], color = 'b', width = 0.25,yerr=F1_std)
F2 = ax.bar(X, data[1], color = 'g', width = 0.25,yerr=F2_std,tick_label=[2017,2018,2019,2020])
F3 = ax.bar(X, data[2], color = 'r', width = 0.25,yerr=F3_std)
ax.legend((F1,F2,F3),('数学','英语','物理'))
plt.show()

上面的代码还可以写成:

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False    # 用来正常显示负号
data = [[30, 25, 50, 20],
[40, 23, 51, 17],
[35, 22, 45, 19]]
X = ['2017','2018','2019','2020']
F1_std = [2,3,4,1]
F2_std = [3,1,2,1]
F3_std = [3,1,2,1]
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
# 参数X是x坐标轴上的标签值,label是为了显示图例的,是每个柱状图的标签。
ax.bar(X, data[0], color='b', width=0.25, label='数学', yerr=F1_std)
ax.bar(X, data[1], color='g', width=0.25, label='英语', yerr=F2_std)
ax.bar(X, data[2], color='r', width=0.25, label = '物理', yerr=F3_std)
ax.legend()
plt.show()

显示结果如下:

在举一个例子:

import numpy as np
import matplotlib.pyplot as pltlabels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 35, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
men_std = [2, 3, 4, 1, 2]
women_std = [3, 5, 2, 3, 3]
width = 0.35       # the width of the bars: can also be len(x) sequencefig, ax = plt.subplots()
# label = 'Men'显示在图例中,labels为x轴的。
ax.bar(labels, men_means, width, yerr=men_std, label='Men')
ax.bar(labels, women_means, width, yerr=women_std, bottom=men_means,label='Women')ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.legend()plt.show()

显示结果如下:

注意上述代码中的一些小变动,尤其注意上述代码中ax.bar()函数里面的参数所代表的含义。

5、条形图横向显示

此处放上一个官方的给的例子,涉及的知识点很多,耐心查看理解,直接上代码:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from collections import namedtuplenp.random.seed(42)Student = namedtuple('Student', ['name', 'grade', 'gender'])
Score = namedtuple('Score', ['score', 'percentile'])# GLOBAL CONSTANTS
test_names = ['Pacer Test', 'Flexed Arm\n Hang', 'Mile Run', 'Agility','Push Ups']
test_meta = dict(zip(test_names, ['laps', 'sec', 'min:sec', 'sec', '']))def attach_ordinal(num):"""Convert an integer to an ordinal string, e.g. 2 -> '2nd'."""suffixes = {str(i): vfor i, v in enumerate(['th', 'st', 'nd', 'rd', 'th','th', 'th', 'th', 'th', 'th'])}v = str(num)# special case early teensif v in {'11', '12', '13'}:return v + 'th'return v + suffixes[v[-1]]def format_score(scr, test):"""Build up the score labels for the right Y-axis by firstappending a carriage return to each string and then tacking onthe appropriate meta information (i.e., 'laps' vs. 'seconds'). Wewant the labels centered on the ticks, so if there is no metainfo (like for pushups) then don't add the carriage return tothe string"""md = test_meta[test]if md:return '{0}\n{1}'.format(scr, md)else:return scrdef format_ycursor(y):y = int(y)if y < 0 or y >= len(test_names):return ''else:return test_names[y]def plot_student_results(student, scores, cohort_size):#  create the figurefig, ax1 = plt.subplots(figsize=(9, 7))fig.subplots_adjust(left=0.115, right=0.88)fig.canvas.set_window_title('Eldorado K-8 Fitness Chart')pos = np.arange(len(test_names))rects = ax1.barh(pos, [scores[k].percentile for k in test_names],align='center',height=0.5,tick_label=test_names)ax1.set_title(student.name)ax1.set_xlim([0, 100])ax1.xaxis.set_major_locator(MaxNLocator(11))ax1.xaxis.grid(True, linestyle='--', which='major',color='grey', alpha=.25)# Plot a solid vertical gridline to highlight the median positionax1.axvline(50, color='grey', alpha=0.25)# Set the right-hand Y-axis ticks and labelsax2 = ax1.twinx()scoreLabels = [format_score(scores[k].score, k) for k in test_names]# set the tick locationsax2.set_yticks(pos)# make sure that the limits are set equally on both yaxis so the# ticks line upax2.set_ylim(ax1.get_ylim())# set the tick labelsax2.set_yticklabels(scoreLabels)ax2.set_ylabel('Test Scores')xlabel = ('Percentile Ranking Across {grade} Grade {gender}s\n''Cohort Size: {cohort_size}')ax1.set_xlabel(xlabel.format(grade=attach_ordinal(student.grade),gender=student.gender.title(),cohort_size=cohort_size))rect_labels = []# Lastly, write in the ranking inside each bar to aid in interpretationfor rect in rects:# Rectangle widths are already integer-valued but are floating# type, so it helps to remove the trailing decimal point and 0 by# converting width to int typewidth = int(rect.get_width())rankStr = attach_ordinal(width)# The bars aren't wide enough to print the ranking insideif width < 40:# Shift the text to the right side of the right edgexloc = 5# Black against white backgroundclr = 'black'align = 'left'else:# Shift the text to the left side of the right edgexloc = -5# White on magentaclr = 'white'align = 'right'# Center the text vertically in the baryloc = rect.get_y() + rect.get_height() / 2label = ax1.annotate(rankStr, xy=(width, yloc), xytext=(xloc, 0),textcoords="offset points",ha=align, va='center',color=clr, weight='bold', clip_on=True)rect_labels.append(label)# make the interactive mouse over give the bar titleax2.fmt_ydata = format_ycursor# return all of the artists createdreturn {'fig': fig,'ax': ax1,'ax_right': ax2,'bars': rects,'perc_labels': rect_labels}student = Student('Johnny Doe', 2, 'boy')
scores = dict(zip(test_names,(Score(v, p) for v, p inzip(['7', '48', '12:52', '17', '14'],np.round(np.random.uniform(0, 100, len(test_names)), 0)))))
cohort_size = 62  # The number of other 2nd grade boysarts = plot_student_results(student, scores, cohort_size)
plt.show()

显示的结果如下:
代码详细解读以后在更。

7、matplotlib条形图的绘制相关推荐

  1. python中matplotlib条形图数值大的在最底层显示_如何使用python的matplotlib模块绘制水平条形图...

    python是一个很有趣的语言,可以在命令行窗口运行.python中有很多功能强大的模块,这篇经验告诉你,如何利用python的matplotlib模块,绘制水平条形图. 工具/原料 windows系 ...

  2. Python 数据分析三剑客之 Matplotlib(六):直方图 / 柱状图 / 条形图的绘制

    CSDN 课程推荐:<Python 数据分析与挖掘>,讲师刘顺祥,浙江工商大学统计学硕士,数据分析师,曾担任唯品会大数据部担任数据分析师一职,负责支付环节的数据分析业务.曾与联想.亨氏.网 ...

  3. python数据分析-matplotlib散点图-条形图的绘制以及完整方法归纳02

    matplotlib的基本使用02 一.散点图的绘制 二.散点图绘图步骤及案例解析 1.导入模块 2.设置散点图所有字符的字体样式 3.编写主体代码 4.主题代码解析 5.图形展示 三.条形图的绘制 ...

  4. python数据可视化(matplotlib条形图、饼图、箱状图、直方图、折线图)(代码)

    python数据可视化(matplotlib条形图.饼图.箱状图.直方图.折线图) matplotlib(条形图) 一.简单条形图 1.简单垂直条形图 2.简单水平条形图 二.水平交错条形图 三.垂直 ...

  5. matplotlib 常用图形绘制与官方文档

    matplotlib 常用图形绘制与官方文档 matplotlib.org Python库matplotlib 图形绘制 官方教程目录 matplotlib.pyplot matplotlib.pyp ...

  6. 【Matplotlib】matplotlib.animation.FuncAnimation绘制动态图、交互式绘图汇总(附官方文档)

    文章目录 零.文中用到的相关知识: 一.以sin举例,motplotlib绘制动图 1.绘制sin函数 2.动态画出sin函数曲线 3.点在曲线上运动 4.点,坐标运动 二.单摆例子 1.scipy中 ...

  7. python Numpy 的基础用法以及 matplotlib 基础图形绘制

    python Numpy 的基础用法以及 matplotlib 基础图形绘制 1. 环境搭建 1.1 Anaconda ​ anaconda 集成了数据分析,科学计算相关的所有常用安装包,比如Numo ...

  8. python使用matplotlib可视化subplots绘制子图、自定义几行几列子图,如果M行N列,那么最终包含M*N个子图、在指定的子图中添加可视化结果

    python使用matplotlib可视化subplots绘制子图.自定义几行几列子图,如果M行N列,那么最终包含M*N个子图.在指定的子图中添加可视化结果 目录

  9. Python dataframe绘制饼图_【python可视化】:matplotlib:常见图表绘制——面积图、填图、饼图...

    •本文字数:约620字•阅读时长:约2分钟•难度:1颗星 斑点鱼将Matplotlib分为五部分: 图表窗口设置.图表基本元素及图表输出.图表样式参数.子图创建.常见图表绘制 本文讲第五部分:matp ...

  10. Python绘图之matplotlib基础教程:matplotlib库图表绘制中常规设置大全(交互模式、清除原有图像、设置横坐标显示文字/旋转角度、添加图例、绘图布局自动调整、图像显示、图像暂停)

    Python绘图之matplotlib基础教程:matplotlib库图表绘制中常规设置大全(交互模式.清除原有图像.设置横坐标显示文字/旋转角度.添加图例.绘图布局自动调整.图像显示.图像暂停) 目 ...

最新文章

  1. CStopwatch的C++实现
  2. 代码编辑神器VIM(附我写acm程序时的配置)(转)
  3. xcode快捷键大全
  4. 腾讯产品课观后感之定位2018-08-22
  5. 微型统计分析系统README
  6. 如何去掉ECShop 2.7.2中的Powered by ECShop字符
  7. 【渝粤教育】国家开放大学2019年春季 1260软件工程 参考试题
  8. python查看字符编码_python如何查看字符集
  9. python打印当前文件的绝对路径,并解决打印为空
  10. mysql 原理 ~ 并行复制
  11. thinkphp 文件下载实例 实现以及注意事项
  12. 应急响应(日志/流量)
  13. 使用Visio2003更新SQL2005数据库
  14. SQL 插入一列数据
  15. linux挂载VMFS硬盘,挂载ESXi服务器VMFS存储卷
  16. 显示器信号时有时无并无html,电脑开机显示器无信号、键盘鼠标不亮解决方法...
  17. 重磅!中国科学技术大学,成立新学院!
  18. (转)360的困兽之斗——重新探讨奇虎商业模式
  19. WINDOWS 安装XGBoost GPU版本最新简易方法
  20. 整理Flutter App开发过程中遇到的问题及解决方法

热门文章

  1. CTR 预估模型简介--深度学习篇
  2. 凸优化第二章凸集 2.1 仿射集合和凸集
  3. 在 Linux 上安装 Go
  4. Python---装饰器
  5. Downloading Quest SQL Optimizer for Oracle
  6. DPM2012学习(二),添加本地磁盘
  7. ASP.NET编程的十大技巧
  8. 并行开发 5.同步机制(下)
  9. 一、基础篇--1.3进程和线程-CountDownLatch、CyclicBarrier 和 Semaphore
  10. Python仿微信红包算法