一、简介
Matplotlib ( 数据可视化工具)画二维图表的python库(直方图、条形图、散点图等)
- mat - matrix 矩阵
- plot - 画图
- lib - library 库
二、操作
(一)折线图
plt.plot(x, y_01, color=“r”, linestyle="-.", label=“y_01”)
import matplotlib.pyplot as plt
import random
# 1、准备数据 x y
x = range(60)
y_01 = [random.uniform(15, 18) for i in x]
y_02 = [random.uniform(1, 3) for i in x]
# 2、创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 3、绘制图像
plt.plot(x, y_01, color="r", linestyle="-.", label="y_01")#label-图例显示
plt.plot(x, y_02, color="b", label="y_02")
# 显示图例
plt.legend()
# 修改x、y刻度
# 准备x的刻度说明
x_label = ["11点{}分".format(i) for i in x]
plt.xticks(x[::5], x_label[::5])
plt.yticks(range(0, 40, 5))
# 添加网格显示
plt.grid(linestyle="--", alpha=0.5)
# 添加描述信息
plt.xlabel("时间")
plt.ylabel("数值")
plt.title("标题")
# 4、显示图
plt.show()
注意:多坐标系显示【plt.subplots】
figure, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=80)
import matplotlib.pyplot as plt
import random
# 1、准备数据 x y
x = range(60)
y_01 = [random.uniform(15, 18) for i in x]
y_02 = [random.uniform(1, 3) for i in x]
# 2、创建画布
figure, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=80)
# 3、绘制图像
axes[0].plot(x, y_01, color="r", linestyle="-.", label="y_01")
axes[1].plot(x, y_02, color="b", label="y_02")
# 显示图例
axes[0].legend()
axes[1].legend()
# 修改x、y刻度
# 准备x的刻度说明
x_label = ["11点{}分".format(i) for i in x]
axes[0].set_xticks(x[::5])
axes[0].set_xticklabels(x_label)
axes[0].set_yticks(range(0, 40, 5))
axes[1].set_xticks(x[::5])
axes[1].set_xticklabels(x_label)
axes[1].set_yticks(range(0, 40, 5))
# 添加网格显示
axes[0].grid(linestyle="--", alpha=0.5)
axes[1].grid(linestyle="--", alpha=0.5)
# 添加描述信息
axes[0].set_xlabel("时间")
axes[0].set_ylabel("数值")
axes[0].set_title("标题01")
axes[1].set_xlabel("时间")
axes[1].set_ylabel("数值")
axes[1].set_title("标题02")
# 4、显示图
plt.show()
(二)散点图绘制
plt.scatter(x, y,color=‘r’)
import matplotlib.pyplot as plt
import random
# 1、准备数据
nn=range(50)
x = [random.uniform(0,100) for i in nn]
y = [random.uniform(10,20) for i in nn]
# 2、创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 3、绘制图像
plt.scatter(x, y,color='r')
# 4、显示图像
plt.show()
(三)柱状图
plt.bar(x, y, color=[‘b’,‘r’,‘g’,‘y’,‘c’,‘m’,‘y’,‘k’,‘c’,‘g’,‘b’])
# 1、准备数据
movie_names = ['电影01','电影02','电影03','电影04','电影05', '电影06','电影07','电影08','电影09','电影10','电影11']
x = range(len(movie_names))
y=[random.uniform(10000,100000) for i in range(len(movie_names))]
# 2、创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 3、绘制柱状图
plt.bar(x, y, color=['b','r','g','y','c','m','y','k','c','g','b'])
# 修改x刻度
plt.xticks(x, movie_names)
# 添加标题
plt.title("电影对比")
# 添加网格显示
plt.grid(linestyle="--", alpha=0.5)
# 4、显示图像
plt.show()
对比柱形图
# 1、准备数据
movie_name = ['movie01','movie02','movie03']
y_01 = [10587,10562.5,1575.7]
y_02=[36456.9,33479.6,11760]
# 2、创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 3、绘制柱状图
plt.bar(range(3), y_01, width=0.3, label="y_01",color='r')
plt.bar([0.3, 1.3, 2.3], y_02, width=0.3, label="y_02",color='g')
# 显示图例
plt.legend()
# 修改刻度
plt.xticks([0.15, 1.15, 2.15], movie_name)
# 4、显示图像
plt.show()
(四)直方图
plt.hist(time, bins=group_num, density=True)
属性 | 说明 | 类型 |
---|---|---|
x | 数据 | 数值 |
bins | 条形数 | int |
color | 颜色 | “r”,“g” |
density | 是否以密度的形式显示 | bool |
range | x轴的范围 | 数值元组(起,终) |
bottom | y轴的起始位置 | 数值类型 |
histtype | 线条的类型 | “bar”:方形,“barstacked”:柱形, “step”:“未填充线条”“stepfilled”:“填充线条” |
align | 对齐方式 | “left”、“mid”、“right” |
orientation | 对齐方式 | “horizontal”、“vertical” |
log | 单位是否以科学计数法 | bool |
# 1、准备数据
time=[int(random.uniform(100,200)) for i in range(100)]
# 2、创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 3、绘制直方图
distance = 2
group_num = int((max(time) - min(time)) / distance)
plt.hist(time, bins=group_num, density=True)
# 修改x轴刻度
plt.xticks(range(min(time), max(time) + 2, distance))
# 添加网格
plt.grid(linestyle="--", alpha=0.5)
# 4、显示图像
plt.show()
(五)饼图
plt.pie(y, labels=movie_name, colors=[‘b’,‘r’,‘g’,‘y’,‘c’,‘m’,‘y’,‘k’,‘c’,‘g’,‘y’], autopct="%1.2f%%")
# 1、准备数据
movie_name = ['电影01','电影02','电影03','电影04','电影05','电影06','电影07','电影08','电影09','电影10','电影11']
y=[random.uniform(10000,100000) for i in range(len(movie_names))]
# 2、创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 3、绘制饼图
plt.pie(y, labels=movie_name, colors=['b','r','g','y','c','m','y','k','c','g','y'], autopct="%1.2f%%")#autopct-显示百分比;labels-【饼图标签】【图例】
# 显示图例
plt.legend()
plt.axis('equal')#让x轴和y轴单位长度相等,即分辨率相等
# 4、显示图像
plt.show()
三、中文乱码问题
代码前加入
#解决中文显示问题
plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
四、补充:
直方图与柱状图的对比
1. 直方图展示数据的分布,柱状图比较数据的大小。
2. 直方图X轴为定量数据,柱状图X轴为分类数据。
3. 直方图柱子无间隔,柱状图柱子有间隔
4. 直方图柱子宽度可不一,柱状图柱子宽度须一致