【技术专题】Matplotlib3 Python 数据可视化 - Matplotlib3 绘制条形图(Bar)

大家好,我是锋哥。最近连载更新《Matplotlib3 Python 数据可视化》技术专题。

本课程讲解利用python进行数据可视化 科研绘图-Matplotlib,学习Matplotlib图形参数基本设置,绘图参数及主要函数,以及Matplotlib基础绘图,和Matplotlib高级绘图。同时也配套视频教程 《2026版 Matplotlib3 Python 数据可视化 视频教程》

Matplotlib 的条形图是展示分类数据最常用的图表之一,特别适合比较不同类别的数值大小。以下是条形图的全面介绍及示例代码:

一、核心功能与适用场景

条形图类型:

  • 垂直条形图plt.bar()
  • 水平条形图plt.barh()
  • 分组条形图:多组数据并列比较
  • 堆叠条形图:显示部分与整体关系

适用场景:

  1. 不同类别数据的比较(产品销量、地区收入)
  2. 时间序列数据对比(月度销售额)
  3. 部分与整体关系可视化(堆叠条形图)

plt.bar() 是 Matplotlib 中用于绘制垂直条形图的核心函数。以下是详细解析及示例:

基本语法:

css 复制代码
plt.bar(x, height, width=0.8, bottom=None, align='center', **kwargs)
参数 说明
x 条形的横坐标位置(标量或数组)
height 条形的高度(y 轴值,标量或数组)
width 条形的宽度(默认 0.8
bottom 条形的起始基线高度 (用于堆叠条形图,默认从 0 开始)
align 对齐方式:'center'(居中,默认)或 'edge'(左对齐)
**kwargs 其他样式参数(颜色、边框等)

常用 kwargs 参数

参数 说明
color / c 条形填充颜色
edgecolor/ec 边框颜色
linewidth/lw 边框宽度
alpha 透明度
label 图例标签
hatch 填充图案(如 '///'

我们先看一个垂直条形图示例:

ini 复制代码
import matplotlib
import matplotlib.pyplot as plt
​
# 设置matplotlib使用黑体显示中文
matplotlib.rcParams['font.family'] = 'Microsoft YaHei'
​
# 数据准备
categories = ['苹果', '橙子', '香蕉', '葡萄', '芒果']
sales = [45, 32, 28, 51, 39]
​
# 创建图形
plt.figure(figsize=(10, 6))
​
# 绘制条形图
bars = plt.bar(
    categories,
    sales,
    color=['#FF6B6B', '#4ECDC4', '#FFD166', '#A06CD5', '#6CA6CD'],
    edgecolor='black',
    linewidth=1.2
)
​
# 添加数据标签
for bar in bars:
    height = bar.get_height()
    plt.text(bar.get_x() + bar.get_width() / 2.,
             height + 0.5,
             f'{height}',
             ha='center',
             va='bottom',
             fontsize=10)
​
# 设置标题和标签
plt.title('水果销售对比', fontsize=14, pad=20)
plt.xlabel('水果类型', fontsize=12)
plt.ylabel('销售量(千克)', fontsize=12)
plt.grid(axis='y', linestyle='--', alpha=0.7)
​
# 调整布局
plt.tight_layout()
plt.show()

运行效果:

我们再看一个水平条形图示例:

ini 复制代码
import matplotlib
import matplotlib.pyplot as plt
​
# 设置matplotlib使用黑体显示中文
matplotlib.rcParams['font.family'] = 'Microsoft YaHei'
​
# 数据准备
categories = ['苹果', '橙子', '香蕉', '葡萄', '芒果']
sales = [45, 32, 28, 51, 39]
​
plt.figure(figsize=(10, 6))
​
# 绘制水平条形图
bars = plt.barh(
    categories,
    sales,
    color='#5F9EA0',
    height=0.7
)
​
# 添加数据标签
for bar in bars:
    width = bar.get_width()
    plt.text(width + 0.8,
             bar.get_y() + bar.get_height() / 2,
             f'{width}',
             va='center',
             fontsize=10)
​
plt.title('水果销售对比', fontsize=14)
plt.xlabel('销售量(千克)', fontsize=12)
plt.ylabel('水果类型', fontsize=12)
plt.grid(axis='x', linestyle='--', alpha=0.7)
plt.tight_layout()
plt.show()

运行效果:

相关推荐
IvanCodes4 小时前
Python 数据处理(十三):JSON、CSV 与数据序列化
开发语言·python
Patrick在香港5 小时前
Claude 工具调用返回空:8 次失败里只有 1 次状态码不对,其余全带 200
爬虫·python·api·claude·香港
Web3&Basketball5 小时前
CRM Agent 后训练实战:3 倍更少错误
python·架构·大模型·agent·推理
AIFQuant6 小时前
ETF行情API接入踩坑记:从报错到跑通的七个问题
python·金融·区块链·etf·基金
GPU实战笔记7 小时前
云端 Python 开发:JupyterLab 还是 VS Code Remote-SSH?
python·vs code·jupyterlab·remote-ssh·远程开发
狗狗狗狗狗乐啊8 小时前
搭一个 AI 对话工作台 AChat:从 0 到可用的完整记录(一)
python·react.js·ai编程
白猫不黑8 小时前
Python实现简易Web弱口令爆破与防护方案
python·web安全·计算机·网络安全·黑客·信息安全·渗透测试
kuuailetianzi9 小时前
Python初识:定位、优势与发展历程
python
guoran_shini9 小时前
LLM微调-训练垂类问答模型
python·lora·sft
basketball6169 小时前
Python FastAPI 介绍以及常用方法
python·fastapi·vllm·ai infra