• Matplotlib库



    #pic_center =400x
    系列文章:


    python+matplotlib绘图线条类型和颜色选择



    环境配置

    【1】 要不要plt.show()
    ipython中可用魔术方法 %matplotlib inline
    pycharm 中必须使用plt.show()

    【2】设置样式

    import matplotlib.pyplot as plt
    
    x = [1, 2, 3, 4]
    y = [1, 4, 9, 16]
    plt.plot(x, y)
    plt.ylabel("squares")
    plt.show()   
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    x = [1, 2, 3, 4]
    y = [1, 4, 9, 16]
    plt.plot(x, y)
    plt.ylabel("squares")
    
    print(plt.style.available[:])
    # ['Solarize_Light2', '_classic_test_patch',
    #  '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh']
    with plt.style.context("_classic_test_patch"): #seaborn-white是其中一种
        plt.plot(x, y)
        plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    在这里插入图片描述
    plt.style绘图风格展示(matplotlib)

    【3】将图像保存为文件
    plt.savefig(‘./static/images/test.jpg’, dpi=750, bbox_inches = ‘tight’)
    也可使用绝对路径,file_path = r’e:\s\t\test.jpg’
    调整dpi值,越高越清晰。
    bbox_inches参数可以使全图完整输出

    x = np.linspace(0,10,100)
    y = np.exp(x)
    print(plt.style.available[:])
    # ['Solarize_Light2', '_classic_test_patch',
    #  '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh']
    with plt.style.context("_classic_test_patch"): #seaborn-white是其中一种
        plt.plot(x, y)
    
        plt.savefig("E:\\deeplean\\MachineLearning\\lihongyi\\2020\my_figure.png",dpi=50)
        plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    1 、折线图

    plt.style.use("seaborn-whitegrid")
    x = np.linspace(0, 2*np.pi, 100)
    plt.plot(x, np.sin(x))
    plt.show()
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    • 绘制多条曲线
    plt.style.use("seaborn-whitegrid")
    x = np.linspace(0, 2*np.pi, 100)
    plt.plot(x, np.sin(x))
    plt.plot(x, np.cos(x))
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    【1】调整线条颜色和风格

    • 调整线条颜色
    x = np.linspace(0, 2*np.pi, 100)
    plt.style.use("seaborn-whitegrid")
    offsets = np.linspace(0, np.pi, 5)
    colors = ["blue", "g", "r", "yellow", "pink"]
    for offset, col in zip(offsets, colors):
        plt.plot(x, np.sin(x-offset), color=col) # color可缩写为c
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    • 调整线条风格
    x = np.linspace(0, 10, 11)
    offsets = list(range(8))
    linestyles = ["solid", "dashed", "dashdot", "dotted", "-", "--", "-.", ":"]
    for offset , linestyle in zip(offsets, linestyles):
        plt.plot(x, x+offset, linestyle=linestyle) # linestyle可简写为ls
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    • 调整线宽
    x = np.linspace(0, 10, 11)
    offsets = list(range(0, 12, 3))
    linewidths = (i*2 for i in  range(1, 5))
    for offset , linewidth in zip(offsets, linewidths):
        plt.plot(x, x+offset,linewidth=linewidth)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    • 调整数据点标记
    x = np.linspace(0, 10, 11)
    offsets = list(range(0, 12, 3))
    markers = ["*", "+", "o", "s"]
    
    for offset , marker in zip(offsets, markers):
        plt.plot(x, x+offset,marker=marker)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    x = np.linspace(0, 10, 11)
    offsets = list(range(0, 12, 3))
    markers = ["*", "+", "o", "s"]
    
    for offset , marker in zip(offsets, markers):
        plt.plot(x, x+offset,marker=marker,markersize=10) # markersize可简写为ms
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    • 颜色跟风格设置的简写
    x = np.linspace(0, 10, 11)
    offsets = list(range(0, 12, 3))
    color_linestyles = ["g-", "b--", "k-.", "r:"]
    for offset , color_linestyle  in zip(offsets, color_linestyles):
        plt.plot(x, x+offset,color_linestyle)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    x = np.linspace(0, 10, 11)
    offsets = list(range(0, 8, 2))
    color_marker_linestyles = ["g*-", "b+--", "ko-.", "rs:"]
    for offset, color_marker_linestyle in zip(offsets, color_marker_linestyles):
        plt.plot(x, x+offset, color_marker_linestyle)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述
    其他用法及颜色缩写、数据点标记缩写等请查看官方文档,如下:
    官方文档

    【2】调整坐标轴

    1、xlim, ylim
    x = np.linspace(0, 2*np.pi, 100)
    plt.plot(x, np.sin(x))
    plt.xlim(-1, 7)
    plt.ylim(-1.5, 1.5)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    2、axis
    x = np.linspace(0, 2*np.pi, 100)
    plt.plot(x, np.sin(x))
    # plt.axis([-2, 8, -2, 2])
    # plt.axis("tight")
    plt.axis("equal")
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述
    plt.axis([a, b, c, d]) 设置x轴的范围为[a, b],y轴的范围为[c, d]
    plt.axis(‘equal’) x,y轴刻度等长
    plt.axis()用法详解

    3、对数坐标
    x = np.logspace(0, 5, 100)
    plt.plot(x, np.log(x))
    plt.xscale("log")
    plt.show()
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    4、调整坐标轴刻度
    x = np.linspace(0, 10, 100)
    plt.plot(x, x**2)
    plt.xticks(np.arange(0, 12, step=1))
    plt.show()
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    x = np.linspace(0, 10, 100)
    plt.plot(x, x**2)
    plt.xticks(np.arange(0, 12, step=1), fontsize=15)
    plt.yticks(np.arange(0, 110, step=10))
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    5、调整刻度样式
    x = np.linspace(0, 10, 100)
    plt.plot(x, x**2)
    plt.tick_params(axis="both", labelsize=15)
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    参数axis的值为’x’、’y’、’both’,分别代表设置X轴、Y轴以及同时设置,默认值为’both’。
    labelsize用于设置刻度线标签的字体大小
    在这里插入图片描述

    【3】设置图形标签

    x = np.linspace(0, 2*np.pi, 100)
    plt.plot(x, np.sin(x))
    plt.title("A Sine Curve", fontsize=20)
    plt.xlabel("x", fontsize=15)
    plt.ylabel("sin(x)", fontsize=15)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    【4】设置图例

    x = np.linspace(0, 2*np.pi, 100)
    plt.plot(x, np.sin(x), "b-", label="Sin")
    plt.plot(x, np.cos(x), "r--", label="Cos")
    plt.ylim(-1.5, 2)
    # plt.legend() #默认位置
    plt.legend(loc="upper center", frameon=True, fontsize=15) #居中
    plt.legend(loc = "best", ncol=2)#图例 分两列显示图例,best根据空白位置,自动挑选合适的空白位置
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    【5】添加文字和箭头

    x = np.linspace(0, 2*np.pi, 100)
    plt.plot(x, np.sin(x), "b-")
    # 在(3.5,0.5)处添加文字
    plt.text(3.5, 0.5, "y=sin(x)", fontsize=15)
    #
    plt.annotate('local min', xy=(1.5*np.pi, -1), xytext=(4.5, 0),
                 arrowprops=dict(facecolor='black', shrink=0.1),)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    2、散点图

    【1】简单散点图

    x = np.linspace(0, 2*np.pi, 20)
    plt.scatter(x, np.sin(x), marker="o", s=30, c="r")    # s 大小  c 颜色
    
    • 1
    • 2

    在这里插入图片描述

    【2】颜色配置

    x = np.linspace(0, 10, 100)
    y = x**2
    plt.scatter(x, y, c=y, cmap="inferno")
    plt.colorbar()
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述
    颜色配置参考官方文档

    【3】根据数据控制点的大小

    x, y, colors, size = (np.random.rand(100) for i in range(4))
    plt.scatter(x, y, c=colors, s=1000*size, cmap="viridis")
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    【4】透明度
    x, y, colors, size = (np.random.rand(100) for i in range(4))
    plt.scatter(x, y, c=colors, s=1000*size, cmap="viridis", alpha=0.3)
    plt.colorbar()
    plt.show()
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    3、柱形图

    【1】简单柱形图

    x = np.arange(1, 6)
    plt.bar(x, 2*x, align="center", width=0.5, alpha=0.5, color='yellow', edgecolor='red')
    plt.tick_params(axis="both", labelsize=13)
    plt.xticks(x, ('G1', 'G2', 'G3', 'G4', 'G5'))
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    x = ('G1', 'G2', 'G3', 'G4', 'G5')
    y = 2 * np.arange(1, 6)
    plt.bar(x, y, align="center", width=0.5, alpha=0.5, color='yellow', edgecolor='red')
    plt.tick_params(axis="both", labelsize=13)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    x = ["G"+str(i) for i in range(5)]
    y = 1/(1+np.exp(-np.arange(5)))
    colors = ['red', 'yellow', 'blue', 'green', 'gray']
    plt.bar(x, y, align="center", width=0.5, alpha=0.5, color=colors)
    plt.tick_params(axis="both", labelsize=13)
    plt.show()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    【2】累加柱形图

    x = np.arange(5)
    y1 = np.random.randint(20, 30, size=5)
    y2 = np.random.randint(20, 30, size=5)
    plt.bar(x, y1, width=0.5, label="man")
    plt.bar(x, y2, width=0.5, bottom=y1, label="women")
    plt.legend()
    plt.show()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    【3】并列柱形图

    x = np.arange(15)
    y1 = x+1
    y2 = y1+np.random.random(15)
    plt.bar(x, y1, width=0.3, label="man")
    plt.bar(x+0.3, y2, width=0.3, label="women")
    
    plt.legend()
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    【4】横向柱形图

    x = ['G1', 'G2', 'G3', 'G4', 'G5']
    y = 2 * np.arange(1, 6)
    plt.barh(x, y, align="center", height=0.5, alpha=0.8, color="blue", edgecolor="red")
    plt.tick_params(axis="both", labelsize=13)
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    4、多子图

    【1】简单多子图

  • 相关阅读:
    Redisson-lock看门狗原理
    centos7 一键安装部署wvp-gb28181-pro
    基于Zookeeper实现的分布式可重入锁
    Spring IoC源码:createBean(中)
    深入解析PHP函数
    SpringBoot SpringBoot 基础篇 4 基于 SpringBoot 的SSMP 整合案例 4.11 表现层数据一致性处理【R对象】
    厉害了,腾讯内部都用的Spring+MyBatis源码手册,实战理论两不误
    vue导航栏下拉菜单(附带展开收缩动画)
    Leetcode 509. 斐波那契数
    sqlite基本操作
  • 原文地址:https://blog.csdn.net/weixin_42382758/article/details/126044362