Matplotlib用法

学习目标:

学习Python中的Matplotlib用法


在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches


def test1():
    # 基础折线图绘制
    # 绘制(0,0),(1,1),(2,1),(3,3)四个点连成的折线
    # x,y的坐标以数组形式存储起来,调用plt.plot(x,y)和plt.show()组合拳展示
    x = [0, 1, 2, 3]
    y = [0, 1, 1, 3]
    plt.plot(x, y)
    plt.show(

在这里插入图片描述

def test2():
    # 修改折线图的颜色 / 线的形状
    x = [0, 1, 2, 3]
    y = [0, 1, 1, 3]
    plt.plot(x, y, "r--")    # 线形为绿色虚线,rgb+折线"-", "o" 为点 “^" 为三角
    plt.axis([1, 6, 0, 5])   # 修改坐标轴x刻度[1,6],y刻度[0,5]
    plt.show()

在这里插入图片描述

def test3():
    # 输入一维数据
    # 默认当做y坐标轴处理,x坐标轴默认为为[0,1,2....]
    y = [1, 1, 1, 1]
    plt.plot(y, "ro")
    plt.show()

在这里插入图片描述

def test4():
    # 将输入的list转化为numpy.array(),可以增加性能
    # 两幅图像默认在同一画布上显示
    t1 = [1, 5, 1, 5]
    t2 = np.array([5, 1, 5, 1])
    plt.plot(t1, "g-")
    plt.plot(t2, "ro")
    plt.show()

在这里插入图片描述

def test5():
    # 画出sin()和cos()图像,并设置网格线
    # 设置曲线属性
    x = np.linspace(0, 10, 200)  # [0,10]分成200份
    line1, line2 = plt.plot(x, np.sin(x), '--', x, np.cos(x), 'g-')
    plt.setp(line1, color='r', linewidth='10.0')
    plt.axis([0, 10, -2, 2])
    plt.grid(True)  # 设置网格线
    plt.show()

在这里插入图片描述

def test6():
    # 增加标注
    # 箭头指示
    x = np.arange(0, 20, 0.01)
    plt.plot(x, x**2)
    plt.xlabel("Sun Raise", color='r', fontsize=20)  # 对x,y轴标注
    plt.ylabel('Perfect', color='y', fontsize=20)
    plt.grid(True)
    plt.title("Figure.1")  # 增加标题
    plt.text(2, 8, "A Text")  # 指定一个点(2,8)添加文字
    # 箭头指示
    # 指定文字, 箭头指定方向,文字显示的坐标, 箭头的属性
    plt.annotate("max value", xy=(20, 400), xytext=(12.5, 400), arrowprops=dict(facecolor="black", shrink=0.05), )
    plt.show()

在这里插入图片描述

def test7():
    # 同一个画板上画多个子图
    fig, axes = plt.subplots(2, 2)  # 构造2*2的画板
    name = ['Anime', 'Comic', 'Game']
    values = [30, 10, 20]
    axes[0, 0].plot(name, values)
    # 设置子图的参数
    axes[0, 0].set(ylim=[-20, 50], title='An Example Axes', ylabel='Y-Axis', xlabel='X-Axis')
    axes[0, 1].bar(name, values)  # 水平柱状图
    axes[1, 0].scatter(name, values)  # 散点图
    axes[1, 1].barh(name, values)  # 垂直柱状图
    plt.show()

在这里插入图片描述

def test8():
    # 绘制圆形、矩形和椭圆
    fig = plt.figure()  # 创建画布
    ax1 = fig.add_subplot(111, aspect="equal")    # 1X1一张图中的第一张, equal为等宽显示
    rec = patches.Rectangle((0, 0), 8, 4)    # 顶点坐标(0,0) 宽w=8, 高h=4
    cir = patches.Circle((8, 8), 2)    # 圆心坐标(8,8) 半径r
    ell = patches.Ellipse((2, 8), 6, 3)    # 椭圆左顶点坐标(2, 8) 长轴6 短轴3
    ax1.add_patch(rec)
    ax1.add_patch(cir)
    ax1.add_patch(ell)
    plt.plot()
    plt.show()

完成于2020年1月8日 主要参考了(https://www.cnblogs.com/zhouzetian/p/12726528.html)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值