Mathematica 如何绘制双纵坐标轴的图像?
来源:http://forum.simwe.com/thread-985179-1-1.html
提问:
现在需要作出这样一张图,如附件
数据共用一个横轴,但纵轴不同。不能直接让某一组数据直接乘一个常数以画到一起。
麻烦大家看一下。本版有过关于双坐标轴的讨论,但好像跟这个情况都不太一样,故再此一问。
解答:
方法一:
Plot[{Sin[x], Cos[x]}, {x, -1, 1}, Frame -> {{True, True}, {True, False}}, FrameTicks -> {{Automatic, {{-0.5, 5}, {0, 10}, {0.5, 15}}}, {Automatic, None}}, FrameLabel -> {{ "Left" , "Right" }, {None, None}}] |
方法二:
可以参考这个帖子:
https://groups.google.com/d/msg/comp.soft-sys.math.mathematica/CKE9Ghn43x8/F1_JnIbCYjsJ
Clear[TwoAxisDateListPlot]; TwoAxisDateListPlot[f_List, g_List, opts : OptionsPattern[]] := Module[{p1, p2, fm, fM, gm, gM, old, new , newg}, p1 = DateListPlot[f, Axes -> True, Frame -> False, PlotRange -> Automatic]; p2 = DateListPlot[g, Axes -> True, Frame -> False, PlotRange -> Automatic]; {fm, fM} = AbsoluteOptions[p1, PlotRange][[1, 2, 2]]; {gm, gM} = AbsoluteOptions[p2, PlotRange][[1, 2, 2]]; old = AbsoluteOptions[p2, Ticks][[1, 2, 2]]; new = Flatten[{Rescale[First[#1], {gm, gM}, {fm, fM}], Rest[#1]}, 1] & /@ old; newg = {#[[1]], Rescale[#[[2]], {gm, gM}, {fm, fM}]} & /@ g; DateListPlot[{f, newg}, Axes -> False, Frame -> True, FrameTicks -> {Automatic, Automatic, None, new }, PlotRange -> {fm, fM}, opts]] Clear[TwoAxisReverseDateListPlot]; TwoAxisReverseDateListPlot[f_List, g_List, opts : OptionsPattern[]] := Module[{p1, p2, fm, fM, gm, gM, old, new , newg}, p1 = DateListPlot[f, Axes -> True, Frame -> False, PlotRange -> Automatic]; p2 = DateListPlot[g, Axes -> True, Frame -> False, PlotRange -> Automatic]; {fm, fM} = AbsoluteOptions[p1, PlotRange][[1, 2, 2]]; {gm, gM} = AbsoluteOptions[p2, PlotRange][[1, 2, 2]]; old = AbsoluteOptions[p2, Ticks][[1, 2, 2]]; new = Flatten[{fM + fm - Rescale[First[#1], {gm, gM}, {fm, fM}], Rest[#1]}, 1] & /@ old; newg = {#[[1]], fM + fm - Rescale[#[[2]], {gm, gM}, {fm, fM}]} & /@ g; DateListPlot[{f, newg}, Axes -> False, Frame -> True, FrameTicks -> {Automatic, Automatic, None, new }, PlotRange -> {fm, fM}, opts]] TwoAxisReverseDateListPlot[f_List, g_List, r_, opts : OptionsPattern[]] := Module[{p1, p2, gg, pg2, m, M, fm, fM, gm, gM, ggm, ggM, old, new , newg}, p1 = DateListPlot[f, Axes -> True, Frame -> False, PlotRange -> Automatic]; gg = g; gg[[All, 2]] /= 1 - r; p2 = DateListPlot[g, Axes -> True, Frame -> False, PlotRange -> Automatic]; pg2 = DateListPlot[gg, Axes -> True, Frame -> False, PlotRange -> Automatic]; {fm, fM} = AbsoluteOptions[p1, PlotRange][[1, 2, 2]]; {m, M} = {fm, 1/r fM}; {gm, gM} = AbsoluteOptions[p2, PlotRange][[1, 2, 2]]; {ggm, ggM} = AbsoluteOptions[pg2, PlotRange][[1, 2, 2]]; old = AbsoluteOptions[pg2, Ticks][[1, 2, 2]]; new = Flatten[{(M + m - Rescale[First[#1], {ggm, ggM}, {m, M}]), Rest[#1]}, 1] & /@ old; newg = {#[[ 1]], (M + m - (1 - r) Rescale[#[[2]], {gm, gM}, {m, M}])} & /@ g; DateListPlot[{f, newg}, Axes -> False, Frame -> True, FrameTicks -> {Automatic, new , None, All}, PlotRange -> {m, M}, opts]] |
方法三:
标签:
mathematica
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架