使用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()

成品图

多y轴图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值