• 使用Matplotlib画多y轴图


    使用Matplotlib画多y轴图

    代码

    import matplotlib.pyplot as plt
    import mpl_toolkits.axisartist as AA
    from mpl_toolkits.axes_grid1 import host_subplot
    
    %matplotlib inline
    config = {
        "font.family": "serif",
        "font.size": 14,
        "mathtext.fontset": "stix",
        "font.serif": ["Times New Roman"],
        "xtick.direction": "in",
        "ytick.direction": "in",
    }
    plt.rcParams.update(config)
    
    host = host_subplot(111, axes_class=AA.Axes)
    # plt.subplots_adjust(right=0.75)
    par1 = host.twinx()
    par2 = host.twinx()
    par3 = host.twinx()
    
    
    # set the position of axes
    host.axis["top"].set_visible(True)
    
    offset = 0
    new_fixed_axis = par1.get_grid_helper().new_fixed_axis
    par1.axis["right"] = new_fixed_axis(loc="right", axes=par1, offset=(offset, 0))
    par1.axis["right"].toggle(all=True)
    
    offset = 50
    new_fixed_axis = par2.get_grid_helper().new_fixed_axis
    par2.axis["right"] = new_fixed_axis(loc="right", axes=par2, offset=(offset, 0))
    par2.axis["right"].toggle(all=True)
    # par2.axis["right"].set_visible(False)
    
    offset = 110
    new_fixed_axis = par3.get_grid_helper().new_fixed_axis
    par3.axis["right"] = new_fixed_axis(loc="right", axes=par3, offset=(offset, 0))
    par3.axis["right"].toggle(all=True)
    
    
    host.set_xlabel("Time (s)", fontsize=config["font.size"] + 2)
    # host.set_ylabel("Shear stress, normal stress (MPa)")
    host.text(-22, 0.35, 'Normal stress, ', color='c', va='center', rotation='vertical', fontsize=config["font.size"] + 2)
    host.text(-22, 0.74, 'shear stress ', color='m', va='center', rotation='vertical', fontsize=config["font.size"] + 2)
    host.text(-22, 1.01, '(MPa)', color='k', va='center', rotation='vertical', fontsize=config["font.size"] + 2)
    par1.set_ylabel("Slip displacement (mm)", fontsize=config["font.size"] + 2)
    par2.set_ylabel("Slip velocity (mm/s)", fontsize=config["font.size"] + 2)
    par3.set_ylabel("Normal displacement (mm)", fontsize=config["font.size"] + 2)
    
    
    # plot
    colors = ["c", "m", "b", "r", "k"]
    p0 = host.plot(
        data2.index,
        data2["垂直応力(1)[MPa]"],
        "-",
        linewidth=1,
        color=colors[0],
        label="Normal stress",
    )
    p0 = host.plot(
        data2.index,
        data2["せん断応力[MPa]"],
        "-",
        linewidth=1,
        color=colors[1],
        label="Shear stress",
    )
    p1 = par1.plot(
        data2.index,
        data2["せん断変位[mm]"],
        linewidth=1,
        color=colors[2],
        label="shear displacement",
    )
    p2 = par2.plot(
        data2.index,
        data2["velocity[mm/s]"],
        linewidth=1,
        color=colors[3],
        label="shear velocity",
    )
    # par2.plot([0, 800], [0, 0], "k-", linewidth=0.8)
    p3 = par3.plot(
        data2.index,
        data2["垂直全平均変位[mm]"],
        linewidth=1,
        color=colors[4],
        label="Normal displacement",
    )
    
    
    # 轴名称颜色
    host.axis["left"].label.set_color(p0[0].get_color())
    par1.axis["right"].label.set_color(p1[0].get_color())
    par2.axis["right"].label.set_color(p2[0].get_color())
    par3.axis["right"].label.set_color(p3[0].get_color())
    
    
    # set limits of the axes
    host.set_xlim(0, 200)
    host.set_ylim(0, 1.2)
    par1.set_ylim(0, 18)
    par2.set_ylim(-0.05, 0.4)
    par3.set_ylim(0, 0.6)
    
    
    par3.plot([45, 45], [0, 20], "k--", alpha=0.5)
    # start and end time point for evaluating normal stiffness
    par3.plot([120, 120], [0, 20], "k--", alpha=0.5, label='Start for computing normal stifness')
    par3.plot([148, 148], [0, 20], "k--", alpha=0.5, label='End for computing normal stifness')
    # start and end time point for shearing dilation
    par3.plot([178, 178], [0, 20], "k--", alpha=0.5, label='Start for computing shear dilation')
    par3.plot([187, 187], [0, 20], "k--", alpha=0.5, label='End for computing shear dilation')
    
    # plt.legend(ncol=1, bbox_to_anchor=(1.45, 0.7), loc=2, borderaxespad=0)
    host.text(3, 1.12,
              '${\\rm JRC=3.21}, \\sigma_{\\rm n}^{\\rm 0}=1 \\ {\\rm MPa}$',
              fontsize=config["font.size"] + 2)
    host.text(-25, 1.185, '(a)', fontsize=config["font.size"] + 2, fontweight='bold')
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124

    成品图

    多y轴图

  • 相关阅读:
    Node.js 零基础入门 Node.js 零基础入门第四天 4.4 在项目中操作MySQL
    学习WiFi,怎么入手?
    出海季收官,速来 Get 全球化发展实操手册
    LiveGBS流媒体平台GB/T28181功能-国标设备通道分享手机PC浏览器观看直播
    [Python图像处理] 使用OpenCV检测对象颜色
    适合短期内想快速上手数字孪生系统的人使用的数字孪生软件推荐
    【HTTPS】运营商劫持、中间人攻击 与 加密
    MacOS下通过命令行启动、关闭Tomcat服务器并验证
    【前端设计模式】之迭代器模式
    源码分析:tb-core 的作用
  • 原文地址:https://blog.csdn.net/m0_45654959/article/details/133949769