You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Mathematica中绘制单X轴双Y轴的双函数折线图?

解决Mathematica双Y轴折线图绘制问题

你用Overlay叠加图的方式容易出现X轴错位、坐标范围不匹配的问题,推荐两种更可靠的实现方式:

方法一:统一范围后用Show合并

先确定两组数据的共同X轴范围,分别绘制两个图并指定相同的X轴范围,最后用Show合并,确保坐标轴对齐:

ZS1 = {{820, 30}, {860, 40}, {880, 50}, {900, 60}, {920, 70}, {930, 80}, {940, 90}};
theta1 = {{980, 0.3}, {960, 0.4}, {920, 0.5}, {880, 0.8}, {800, 1.1}};

(* 计算两组数据的X轴极值,确定共同范围 *)
xRange = {Min[Flatten[{ZS1[[All, 1]], theta1[[All, 1]]}]], Max[Flatten[{ZS1[[All, 1]], theta1[[All, 1]]}]]};

(* 绘制左侧Y轴的折线 *)
plot1 = ListLinePlot[ZS1, PlotStyle -> Blue, 
  Frame -> {True, True, False, False}, 
  ImagePadding -> 25, 
  FrameStyle -> {Automatic, Blue, Automatic, Automatic},
  PlotRange -> {xRange, Automatic}];

(* 绘制右侧Y轴的折线,隐藏多余坐标轴 *)
plot2 = ListLinePlot[theta1, PlotStyle -> Red, 
  ImagePadding -> 25, 
  Frame -> {False, False, False, True}, 
  FrameTicks -> {{None, None}, {None, All}}, 
  FrameStyle -> {Automatic, Automatic, Automatic, Red},
  PlotRange -> {xRange, Automatic},
  Axes -> False];

(* 合并两个图,显示完整框架 *)
Show[plot1, plot2, Frame -> {True, True, False, True}]

方法二:单ListLinePlot直接实现双Y轴

更简洁的方式是直接将两组数据传入ListLinePlot,通过Frame和FrameTicks配置双Y轴:

ZS1 = {{820, 30}, {860, 40}, {880, 50}, {900, 60}, {920, 70}, {930, 80}, {940, 90}};
theta1 = {{980, 0.3}, {960, 0.4}, {920, 0.5}, {880, 0.8}, {800, 1.1}};

ListLinePlot[{ZS1, theta1},
  PlotStyle -> {Blue, Red},
  Frame -> {True, True, False, True},
  FrameStyle -> {Automatic, Blue, Automatic, Red},
  FrameTicks -> {
    {Automatic, None},    (* 左侧Y轴对应第一组数据 *)
    {Automatic, Automatic}, (* X轴 *)
    {None, None},
    {None, Automatic}     (* 右侧Y轴对应第二组数据 *)
  },
  ImagePadding -> 25,
  PlotLegends -> {"ZS1", "theta1"} (* 可选:添加图例区分两组数据 *)
]

这两种方法都能保证X轴共用、双Y轴独立显示,避免Overlay带来的错位问题。

内容的提问来源于stack exchange,提问作者Angel Kuan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 12:15:47