本课程旨在教授如何使用Matplotlib绘制基本图表,包括折线图、柱状图和散点图。课程将结合实际案例,帮助学生将抽象数据转化为直观可视化结果。
定义:Matplotlib是Python中最基础、最常用的数据可视化库,可以创建各种静态、动态和交互式的图表。
特点:
pip install matplotlib
pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # Windows系统
# 或['Arial Unicode MS'] # Mac系统
plt.rcParams['axes.unicode_minus'] = False
# # 准备数据
x = [1, 2, 3, 4]
y = [10, 15, 13, 17]
# 创建画布
plt.figure(figsize=(8,5))
# 绘制图表
plt.plot(x, y)
# 添加修饰
plt.title("示例图表")
plt.xlabel("X轴标签")
plt.ylabel("Y轴标签")
# 显示图表
plt.show()
案例:
年度GDP增长趋势
人口出生率变化
图书出版数量变化
大学生就业率变化
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # Windows系统
# 或['Arial Unicode MS'] # Mac系统
plt.rcParams['axes.unicode_minus'] = False
# 创建数据
years = np.arange(2010, 2021) # 使用NumPy生成2010-2020的年份序列
enrollment_rates = [56.7, 58.2, 60.1, 62.3, 64.5, 66.8, 69.2, 71.5, 73.8, 75.9, 77.8] # 对应年份的录取率
# 创建折线图
plt.figure(figsize=(10, 6)) # 设置图形大小(宽10英寸,高6英寸)
plt.plot(
years, # X轴数据
enrollment_rates, # Y轴数据
marker='o', # 数据点标记为圆形
color='b', # 线条颜色为蓝色
linestyle='-', # 实线样式
linewidth=2 # 线宽为2磅
)
# 添加标题和标签
plt.title('2010-2020年中国高等教育录取率变化趋势', fontsize=16)
plt.xlabel('年份', fontsize=12)
plt.ylabel('录取率(%)', fontsize=12)
# 添加网格线
plt.grid(
True, # 显示网格
linestyle='--', # 虚线样式
alpha=0.7 # 透明度70%
)
# 显示图表
plt.show()
marker:数据点标记样式(o圆形,s方形,^三角形等)
color:支持颜色名称('b'=blue)、十六进制值('#FF5733')或RGB元组
linestyle:线条样式(-实线,--虚线,:点线等)
linewidth:线条粗细(以磅为单位)
案例:
不同专业学生人数对比
各地区文化遗产数量
各出版社学术著作出版量
不同年龄段阅读习惯调查
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # Windows系统
# 或['Arial Unicode MS'] # Mac系统
plt.rcParams['axes.unicode_minus'] = False
# 创建数据
majors = ['文学', '历史', '哲学', '社会学', '心理学']
students = [320, 180, 150, 240, 210]
# 创建柱状图
plt.figure(figsize=(10, 6))
bars = plt.bar(
majors, # X轴类别
students, # 柱体高度
color=['#4C72B0', '#55A868', '#C44E52', '#8172B2', '#CCB974'] # 自定义颜色
)
# 添加数值标签
for bar in bars:
height = bar.get_height() # 获取每个柱体的高度
plt.text(
bar.get_x() + bar.get_width()/2., # 文本X位置(柱体中心)
height, # 文本Y位置
f'{height}', # 显示数值
ha='center', # 水平居中
va='bottom' # 垂直底部对齐
)
# 添加标题和标签
plt.title('各专业学生人数分布', fontsize=16)
plt.xlabel('专业', fontsize=12)
plt.ylabel('学生人数', fontsize=12)
Text(0, 0.5, '学生人数')
width:控制柱体宽度(默认0.8)
align:对齐方式('center'或'edge')
alpha:透明度(0-1之间)
edgecolor:柱体边框颜色
案例:
阅读时间与成绩关系
收入水平与文化消费关系
城市人口与公共设施数量关系
社交媒体使用与幸福感关系
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # Windows系统
# 或['Arial Unicode MS'] # Mac系统
plt.rcParams['axes.unicode_minus'] = False
# 创建模拟数据
np.random.seed(42) # 设置随机种子确保可重复性
reading_hours = np.random.normal(10, 3, 100) # 生成100个正态分布的阅读时间数据
exam_scores = 50 + 2*reading_hours + np.random.normal(0, 5, 100) # 生成与阅读时间相关的成绩数据
# 创建散点图
plt.figure(figsize=(10, 6))
plt.scatter(
reading_hours, # X轴数据
exam_scores, # Y轴数据
color='green', # 点颜色
alpha=0.6, # 透明度
edgecolors='w' # 点边缘颜色
)
# 添加趋势线
z = np.polyfit(reading_hours, exam_scores, 1) # 一元线性拟合
p = np.poly1d(z) # 生成拟合函数
plt.plot(reading_hours, p(reading_hours), "r--") # 绘制红色虚线趋势线
# 添加标题和标签
plt.title('每周阅读时间与考试成绩关系', fontsize=16)
plt.xlabel('每周阅读时间(小时)', fontsize=12)
plt.ylabel('考试成绩(分)', fontsize=12)
plt.grid(True, linestyle='--', alpha=0.3) # 添加半透明虚线网格
s:控制点的大小
c:可以传入数组实现颜色映射
marker:点的形状(默认圆形)
cmap:颜色映射方案
import matplotlib.pyplot as plt
import pandas as pd
# 假设数据已加载为DataFrame,列名正确
df = pd.read_csv('C:/Users/Zhouq/Desktop/iris_np.csv')
plt.figure(figsize=(10, 6))
for species in df['Species'].unique():
species_data = df[df['Species'] == species]
plt.plot(species_data.index, species_data['Sepal.Length'], label=species)
plt.xlabel('Sample Index')
plt.ylabel('Sepal Length (cm)')
plt.title('Sepal Length Variation by Species')
plt.legend()
plt.show()
C:\Users\Zhouq\AppData\Roaming\Python\Python39\site-packages\pandas\core\computation\expressions.py:21: UserWarning: Pandas requires version '2.8.4' or newer of 'numexpr' (version '2.8.3' currently installed). from pandas.core.computation.check import NUMEXPR_INSTALLED C:\Users\Zhouq\AppData\Roaming\Python\Python39\site-packages\pandas\core\arrays\masked.py:60: UserWarning: Pandas requires version '1.3.6' or newer of 'bottleneck' (version '1.3.5' currently installed). from pandas.core import (
要求:比较三个物种在四个特征(萼片长度、萼片宽度、花瓣长度、花瓣宽度)上的平均值,绘制四个子图,每个子图显示一个特征的物种对比柱状图。
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('iris_np.csv')
means = df.groupby('Species').mean() # 计算各物种均值
features = ['Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width']
plt.figure(figsize=(12, 8))
for i, feature in enumerate(features, 1):
plt.subplot(2, 2, i)
plt.bar(means.index, means[feature], color=['blue', 'green', 'red'])
plt.title(f'Average {feature}')
plt.ylabel('cm')
plt.tight_layout()
plt.show()
要求:绘制萼片长度(Sepal.Length)与花瓣长度(Petal.Length)的散点图,点颜色表示不同物种,添加图例、标题和轴标签。
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('iris_np.csv')
colors = {'setosa': 'blue', 'versicolor': 'green', 'virginica': 'red'}
plt.figure(figsize=(10, 6))
for species, color in colors.items():
species_data = df[df['Species'] == species]
plt.scatter(species_data['Sepal.Length'], species_data['Petal.Length'],
color=color, label=species, alpha=0.7)
plt.xlabel('Sepal Length (cm)')
plt.ylabel('Petal Length (cm)')
plt.title('Sepal vs Petal Length by Species')
plt.legend()
plt.show()
请参考上述示例,完成相应图表作业,注意根据数据特性选择合适的图表类型,并合理美化图表。
| 图表类型 | 适用场景 | 人文社科示例 |
|---|---|---|
| 折线图 | 趋势分析 | 人口变化、经济指标 |
| 柱状图 | 比较分类 | 不同地区文化设施数量 |
| 散点图 | 关系分析 | 教育投入与成果关系 |
Q1:如何保存高清图表?
plt.savefig('output.png',
dpi=300, # 分辨率
bbox_inches='tight', # 去除白边
transparent=True) # 透明背景(可选)
Q2:中文显示异常怎么办?
# 解决方案一:指定中文字体
plt.rcParams['font.sans-serif'] = ['SimHei'] # Windows
# plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # Mac
# 解决方案二:使用具体字体路径
# import matplotlib.font_manager as fm
# font_path = 'your_chinese_font.ttf'#替换自己的字体
# font_prop = fm.FontProperties(fname=font_path)
# plt.title("标题", fontproperties=font_prop)
Q3:如何调整图例样式?
plt.legend(
loc='upper left', # 位置设置
frameon=False, # 去除边框
ncol=2, # 分列显示
prop={'size': 10} # 字体大小
)
Q4:图表元素太多太拥挤?
plt.tight_layout() # 自动调整布局
# 或手动调整
plt.subplots_adjust(
left=0.1,
right=0.9,
top=0.9,
bottom=0.2
)