matplotlib

简介: 【6月更文挑战第9天】

matplotlib.pyplot 是 Python 中一个用于创建静态、动画和交互式可视化的库,特别适合用于科学计算。它提供了一个类似于 MATLAB 的绘图框架,简化了图表的创建和自定义。以下是对 matplotlib.pyplot 库的详细介绍。

基本概念

  1. pyplotmatplotlib.pyplot 是 Matplotlib 库的一个子库,提供了一个类似 MATLAB 的绘图 API。通过这个 API,可以方便地创建各种类型的图表。
  2. Figure:图形的容器,可以包含一个或多个子图(Axes)。
  3. Axes:图的实际区域,一个 Figure 可以包含多个 Axes。
  4. Axis:指的是 x 轴和 y 轴,每个 Axes 对象有两个 Axis 对象(一个用于 x 轴,一个用于 y 轴)。

安装

如果尚未安装 Matplotlib,可以通过以下命令安装:

pip install matplotlib

基本用法

以下是一些常用的 matplotlib.pyplot 操作。

导入库

import matplotlib.pyplot as plt

创建简单的折线图

# 准备数据
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

# 创建图形
plt.figure()

# 绘制图形
plt.plot(x, y)

# 添加标题和标签
plt.title("Simple Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# 显示图形
plt.show()

创建多个子图

# 准备数据
x = [1, 2, 3, 4]
y1 = [10, 20, 25, 30]
y2 = [30, 25, 20, 10]

# 创建图形和子图
fig, axs = plt.subplots(2)

# 绘制第一个子图
axs[0].plot(x, y1)
axs[0].set_title('First Subplot')

# 绘制第二个子图
axs[1].plot(x, y2)
axs[1].set_title('Second Subplot')

# 显示图形
plt.tight_layout()
plt.show()

添加图例

# 准备数据
x = [1, 2, 3, 4]
y1 = [10, 20, 25, 30]
y2 = [30, 25, 20, 10]

# 创建图形
plt.figure()

# 绘制图形
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')

# 添加标题和标签
plt.title("Line Plot with Legend")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# 添加图例
plt.legend()

# 显示图形
plt.show()

常见图表类型

折线图(Line Plot)

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y)
plt.title("Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

散点图(Scatter Plot)

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.scatter(x, y)
plt.title("Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

条形图(Bar Plot)

x = ['A', 'B', 'C', 'D']
y = [10, 20, 25, 30]

plt.bar(x, y)
plt.title("Bar Plot")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()

直方图(Histogram)

data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]

plt.hist(data, bins=4)
plt.title("Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

饼图(Pie Chart)

labels = ['A', 'B', 'C', 'D']
sizes = [10, 20, 30, 40]

plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title("Pie Chart")
plt.show()

自定义图表

Matplotlib 提供了丰富的自定义选项,可以对图表进行详细的调整和美化。

自定义颜色和样式

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y, color='green', linestyle='--', marker='o')
plt.title("Custom Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

添加注释

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y)
plt.title("Annotated Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# 添加注释
plt.annotate('Highest Point', xy=(4, 30), xytext=(3, 25),
             arrowprops=dict(facecolor='black', shrink=0.05))

plt.show()

设置坐标轴范围和刻度

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y)
plt.title("Custom Axis Range and Ticks")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# 设置坐标轴范围
plt.xlim(0, 5)
plt.ylim(0, 35)

# 设置刻度
plt.xticks([0, 1, 2, 3, 4, 5])
plt.yticks([0, 10, 20, 30, 40])

plt.show()

保存图表

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y)
plt.title("Save Plot Example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# 保存图表为 PNG 文件
plt.savefig('plot.png')

# 显示图表
plt.show()
目录
相关文章
|
11月前
|
存储 自然语言处理 文字识别
开放应用架构,建设全新可精细化运营的百炼
本文介绍了阿里云智能集团在百炼大模型应用中的技术实践和运营经验。主要内容包括:1) RAG技术的背景及其在落地时面临的挑战;2) 多模态多语言RAG技术的研发与应用;3) 多模态多元embedding和rank模型的训练;4) 基于千问大模型的embedding和rank模型;5) 开源社区推出的GT千问系列模型;6) 模型应用中的可运营实践;7) AI运营的具体方法论和实践经验。通过这些内容,展示了如何解决实际应用中的复杂需求,提升系统的准确性和用户体验。
|
存储 编译器 Python
python文件处理-CSV文件的读取、处理、写入
python文件处理-CSV文件的读取、处理、写入
1032 0
python文件处理-CSV文件的读取、处理、写入
|
存储 计算机视觉
OpenCV 中 CV_8UC1,CV_32FC3,CV_32S等参数的含义
OpenCV 中 CV_8UC1,CV_32FC3,CV_32S等参数的含义
999 3
|
机器学习/深度学习 并行计算 安全
网络入侵检测 Network Intrusion Detection System (NIDS)
网络入侵检测 Network Intrusion Detection System (NIDS)
|
存储 JSON JavaScript
【Python】csv与json,哪个才是你的数据之选?
【Python】csv与json,哪个才是你的数据之选?
1342 0
|
iOS开发 MacOS Windows
视频 | 把万彩动画中的卡通人物放入PPT中
我试过window下的剪辑软件pr、剪映、达芬奇,都不能成功导出透明背景的GIF图片,最后在MacOS下用Final cut pro和Compressor成功导出了。
495 1
|
Ubuntu
笔记本Ubuntu 设置合盖不自动休眠
经测试,适用于ubuntu 16.04 / 18.04 / 20.04
3380 0
【约束布局】ConstraintLayout 之 Chains 链式约束 ( Chains 简介 | 代码 及 布局分析 | 链头设置 | 间距设置 | 风格设置 | 权重设置 )(三)
【约束布局】ConstraintLayout 之 Chains 链式约束 ( Chains 简介 | 代码 及 布局分析 | 链头设置 | 间距设置 | 风格设置 | 权重设置 )(三)
469 0
【约束布局】ConstraintLayout 之 Chains 链式约束 ( Chains 简介 | 代码 及 布局分析 | 链头设置 | 间距设置 | 风格设置 | 权重设置 )(三)
|
移动开发 JavaScript 前端开发
PhysicsJS:基于JavaScript的强大的物理引擎
PhysicsJS是一个基于JavaScript、模块化、可扩展、易于使用的物理引擎。
873 0
PhysicsJS:基于JavaScript的强大的物理引擎
|
存储 监控 大数据
阿里云InfluxDB®教你玩转A股数据
阿里云InfluxDB®目前已经商业化,专注于处理高写入和查询负载的时序数据,用于存储大规模的时序数据并进行实时分析,包括来自DevOps监控、车联网、智慧交通、金融和IOT传感器数据采集。金融中股票交易具有高频和时间属性,非常符合InfluxDB的应用场景。
11874 0