博客
关于我
【Matplotlib】(一)绘制图像函数
阅读量:464 次
发布时间:2019-03-06

本文共 2256 字,大约阅读时间需要 7 分钟。

Matplotlib绘图库入门

Matplotlib 是 Python 中最著名的绘图库之一,它提供了类似于 MATLAB 的命令 API,非常适合用于交互式绘图。Matplotlib 的文档非常全面,并配有丰富的 Gallery 页面,里面有上百幅缩略图,每幅图都附有源代码。因此,只要你需要绘制某种类型的图像,只需在 Gallery 中浏览、复制、粘贴即可,基本上都能轻松完成。

1.1 代码实现

为了实现绘制函数 ( y = x^2 ) 的图像,我们可以按照以下步骤进行:

  • 导入 Matplotlib 和 NumPy 库:

    import matplotlib.pyplot as plt
    import numpy as np
  • 定义 x 变量的范围 (-3, 3),数量为 50:

    x = np.linspace(-3, 3, 50)
    y = x ** 2
  • 创建一个 Figure 对象并指定大小:

    plt.figure(num=3, figsize=(8, 5))
  • 绘制曲线,设置颜色为红色,线宽度为 1,线型为虚线:

    plt.plot(x, y, color='red', linewidth=1.0, linestyle='--')
  • 设置 x 和 y 轴的范围及标注:

    plt.xlim(-1, 2)
    plt.ylim(-2, 3)
    plt.xlabel('x')
    plt.ylabel('y')
  • 设置坐标轴刻度:

    new_ticks = np.linspace(-1, 2, 5)
    plt.xticks(new_ticks)
    plt.yticks([-2.2, -1, 1, 1.5, 2.4], [r'$really bad$', r'$bad$', r'$normal$', r'$good$', r'$really good$'])
  • 隐藏坐标轴边框并调整位置:

    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.spines['bottom'].set_position(('data', 0))
    ax.yaxis.set_ticks_position('left')
    ax.spines['left'].set_position(('data', 0))
  • 设置标题:

    ax.set_title('y = x^2', fontsize=14, color='r')
  • 显示图像:

    plt.show()
  • 1.2 基本构成

    在 Matplotlib 中,图像由 Figure 对象和 Axes 对象组成。每个 Axes 对象都有独立的坐标系统。关系如下:

    • Title:图像标题
    • Axis:坐标轴
    • Label:坐标轴标注
    • Tick:刻度线
    • Tick Label:刻度注释

    2.1 绘制简单图像

    要绘制简单的图像,可以按照以下步骤进行:

  • 导入必要的模块:

    import matplotlib.pyplot as plt
    import numpy as np
  • 定义 x 变量的范围:

    x = np.linspace(-3, 3, 50)
    y = x ** 2
  • 创建 Figure 并绘制曲线:

    plt.figure()
    plt.plot(x, y)
    plt.show()
  • 2.2 设置 Figure 图像

    为了更好地定制图像,可以按照以下步骤进行:

  • 创建 Figure 对象并指定大小:

    plt.figure(num=3, figsize=(8, 5))
  • 绘制曲线并设置属性:

    plt.plot(x, y, color='red', linewidth=1.0, linestyle='--')
  • 2.3 设置坐标轴

    为了使图像更加精确,可以按照以下步骤进行:

  • 设置 x 轴范围及刻度:

    new_ticks = np.linspace(-1, 2, 5)
    plt.xticks(new_ticks)
  • 设置 y 轴范围及刻度:

    plt.yticks([-2.2, -1, 1, 1.5, 2.4], [r'$really bad$', r'$bad$', r'$normal$', r'$good$', r'$really good$'])
  • 2.4 移动坐标轴

    为了使坐标轴更加美观,可以按照以下步骤进行:

  • 获取当前坐标轴:

    ax = plt.gca()
  • 隐藏边框:

    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
  • 调整坐标轴位置:

    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
  • 设置边框位置:

    ax.spines['bottom'].set_position(('data', 0))
    ax.spines['left'].set_position(('data', 0))
  • 2.5 设置标题

    最后,可以设置图像标题:

    ax.set_title('y = x^2', fontsize=14, color='r')

    通过以上步骤,你可以轻松地使用 Matplotlib 绘制出高质量的图像,并进行各种定制。

    转载地址:http://jlpfz.baihongyu.com/

    你可能感兴趣的文章
    mysql -存储过程
    查看>>
    mysql /*! 50100 ... */ 条件编译
    查看>>
    mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
    查看>>
    mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
    查看>>
    mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
    查看>>
    mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
    查看>>
    MySQL 8.0 恢复孤立文件每表ibd文件
    查看>>
    MySQL 8.0开始Group by不再排序
    查看>>
    mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
    查看>>
    multi swiper bug solution
    查看>>
    MySQL Binlog 日志监听与 Spring 集成实战
    查看>>
    MySQL binlog三种模式
    查看>>
    multi-angle cosine and sines
    查看>>
    Mysql Can't connect to MySQL server
    查看>>
    mysql case when 乱码_Mysql CASE WHEN 用法
    查看>>
    Multicast1
    查看>>
    mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
    查看>>
    MySQL Cluster 7.0.36 发布
    查看>>
    Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
    查看>>
    MySQL Cluster与MGR集群实战
    查看>>