Matplotlib基本操作

在这里插入图片描述

1.什么是Matplotlib

  matplotlib 是一个广泛使用的 Python 图形库,用于生成静态、动态和交互式的可视化图表。它最初由 John D. Hunter 创建,并首次发布于2003年。matplotlib 提供了一个面向对象的 API,允许用户创建多种类型的图表,包括线图、散点图、直方图、功率谱、条形图、误差图、饼图等。

  matplotlib 的核心特性包括:

  灵活性:用户可以控制线条样式、字体属性、布局调整等细节。
  输出格式:支持多种图形输出格式,如 PNG、PDF、SVG、EPS 和 PGF。 3 交互性:可以嵌入到 GUI 应用程序中,如 Tkinter、wxPython、Qt 等。
  兼容性:可以在多种操作系统上运行,包括 Windows、Mac OS 和 Linux。
  扩展性:可以通过各种插件和第三方库进行扩展,以实现更高级的功能。

  matplotlib 的主要组成部分包括:
  pyplot:这是一个类似于 MATLAB 的模块,提供了许多用于快速生成图表的函数。
  Artist API:这是一种更强大的面向对象接口,允许用户精确控制图表的每个细节。

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible. --官方描述
https://matplotlib.org/stable/ --官网

2.Matplotlib安装

pip install matplotlib

3.作图

3.1.曲线图

import matplotlib.pyplot as plt
import numpy as np

# 创建数据
x = np.linspace(0, 10, 100)  # 生成从0到10的100个点
y = np.sin(x)  # 计算每个点的正弦值

# 绘制曲线图
plt.plot(x, y, label='sin(x)')

# 添加标题和轴标签
plt.title('Sine Wave')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')

# 添加图例
plt.legend()

# 显示网格
plt.grid(True)

# 显示图表
plt.show()

在这里插入图片描述

3.2.折线图

import matplotlib.pyplot as plt

# 数据
months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
sales = [1200, 1500, 1600, 1400, 1700, 1800, 2000, 2200, 2100, 1900, 2300, 2400]

# 创建折线图
plt.figure(figsize=(10, 5))
plt.plot(months, sales, marker='o', linestyle='-', color='b')

# 设置图表标题和坐标轴标签
plt.title('Monthly Sales Data')
plt.xlabel('Month')
plt.ylabel('Sales')

# 设置 x 轴刻度标签
plt.xticks(months)

# 显示网格
plt.grid(True)

# 显示图表
plt.show()

在这里插入图片描述

3.3.柱状图

import matplotlib.pyplot as plt

# 数据
months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
sales = [1200, 1500, 1600, 1400, 1700, 1800, 2000, 2200, 2100, 1900, 2300, 2400]

# 创建柱状图
plt.figure(figsize=(10, 5))
plt.bar(months, sales, color='blue')

# 设置图表标题和坐标轴标签
plt.title('Monthly Sales Data')
plt.xlabel('Month')
plt.ylabel('Sales')

# 设置 x 轴刻度标签
plt.xticks(months)

# 显示网格
plt.grid(axis='y')

# 显示图表
plt.show()

在这里插入图片描述

3.4.热力图

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# 设置随机种子以确保每次运行的结果相同
np.random.seed(0)

# 创建一个5x5的随机矩阵
data = np.random.rand(5, 5)

# 使用seaborn库绘制热力图
plt.figure(figsize=(8, 6))
sns.heatmap(data, annot=True, fmt=".2f", cmap='coolwarm', cbar=True)
plt.title('Heatmap of Random Data')
plt.show()

在这里插入图片描述

3.5.箱线图

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# 设置随机种子以确保每次运行的结果相同
np.random.seed(0)

# 创建两组随机数据
data_group1 = np.random.normal(loc=0.0, scale=1.0, size=100)
data_group2 = np.random.normal(loc=1.0, scale=1.0, size=100)

# 将数据合并到一个字典中,方便绘图
data_dict = {'Group 1': data_group1, 'Group 2': data_group2}

# 使用seaborn库绘制箱线图
plt.figure(figsize=(8, 6))
sns.boxplot(data=data_dict)
plt.title('Box Plot of Two Data Groups')
plt.ylabel('Value')
plt.xlabel('Groups')
plt.show()

在这里插入图片描述

3.6.3d图表

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 创建一个3D图形
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')

# 生成数据
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
u, v = np.meshgrid(u, v)
x = 10 * np.cos(u) * np.sin(v)
y = 10 * np.sin(u) * np.sin(v)
z = 10 * np.cos(v)

# 绘制3D表面图
surf = ax.plot_surface(x, y, z, cmap='viridis')

# 添加颜色条
fig.colorbar(surf, shrink=0.5, aspect=5)

# 设置坐标轴标签
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

# 显示图形
plt.show()

在这里插入图片描述

3.7.动态图

import itertools

import matplotlib.pyplot as plt
import numpy as np

import matplotlib.animation as animation


def data_gen():
    for cnt in itertools.count():
        t = cnt / 10
        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)


def init():
    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(0, 1)
    del xdata[:]
    del ydata[:]
    line.set_data(xdata, ydata)
    return line,

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.grid()
xdata, ydata = [], []


def run(data):
    # update the data
    t, y = data
    xdata.append(t)
    ydata.append(y)
    xmin, xmax = ax.get_xlim()

    if t >= xmax:
        ax.set_xlim(xmin, 2*xmax)
        ax.figure.canvas.draw()
    line.set_data(xdata, ydata)

    return line,

# Only save last 100 frames, but run forever
ani = animation.FuncAnimation(fig, run, data_gen, interval=100, init_func=init,
                              save_count=100)
plt.show()

在这里插入图片描述

4.保存图片

import matplotlib.pyplot as plt
import numpy as np

# 创建数据
x = np.linspace(0, 10, 100)
y = np.sin(x)

# 创建一个新的图形窗口
fig, ax = plt.subplots()

# 绘制折线图
ax.plot(x, y)

# 设置标题和坐标轴标签
ax.set_title('Sine Wave')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')

# 保存图像
plt.savefig('sine_wave_plot.png')

# 显示图形
plt.show()

在这里插入图片描述

5.结语

  matplotlib是一个可视化python库,可以方便快捷的做出相对应的数据图表,不过可能风格偏科研,如果追求图表的炫酷美观,可以尝试Echarts等web图表。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的体育馆管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本体育馆管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此体育馆管理系统利用当下成熟完善的SpringBoot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线选择试题并完成答题,在线查看考核分数。管理员管理收货地址管理、购物车管理、场地管理、场地订单管理、字典管理、赛事管理、赛事收藏管理、赛事评价管理、赛事订单管理、商品管理、商品收藏管理、商品评价管理、商品订单管理、用户管理、管理员管理等功能。体育馆管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:体育馆管理系统;SpringBoot框架;Mysql;自动化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海若[MATRIX]

鼓励将是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值