프로그래밍 언어/Mathematica

[Mathematica] Generate Plots with two vertical scales

by UltraLowTemp-Physics 2024. 2. 12.

https://reference.wolfram.com/language/howto/GeneratePlotsWithTwoVerticalScales.html    

 

 

1. `ImagePadding` + `Overlay` 

plt1 = Plot[x^3, {x, 0, 10},
   PlotStyle -> Blue,
   ImagePadding -> 50,
   Frame -> {True, True, True, False},
   FrameStyle -> Blue, PlotStyle -> Blue,
   FrameLabel -> {{"y (first plot)", 
      "y (second plot)"}, {"x (common)", None}}];

plt2 = Plot[Exp[x], {x, 0, 10},
   ScalingFunctions -> "Log",
   ImagePadding -> 50,
   Frame -> {False, False, False, True},
   FrameTicks -> {{None, All}, {None, None}},
   FrameStyle -> Red,
   FrameLabel -> {{None, "y (second plot)"}, {None, None}}
   ];

Overlay[{plt1, plt2}]

 

2. ResourceFunction["CombinedPlots"]

• URL: https://resources.wolframcloud.com/FunctionRepository/resources/CombinePlots   
Personally, this method would be the most easiest and straightforward to make two axis in a single plot. This function is user-defined function (i.g., not built from Mathematica). However, it contains most of essentional parts required.  The below one is an example used above the function. 

ResourceFunction["CombinePlots"][
 Plot[x^3, {x, 0, 10}, PlotStyle -> Blue, Frame -> True, 
  FrameStyle -> Blue, FrameLabel -> {"", ""}],
 Plot[Exp[x], {x, 0, 10},
  ScalingFunctions -> "Log",
  Frame -> True, FrameStyle -> Red, PlotStyle -> Red,
  FrameLabel -> {"", ""}
  ], "AxesSides" -> "TwoY",
 FrameLabel -> {Style["x (common axis)", 14], 
   Style["y (first plot)", 14], None, Style["y (second plot)", 14]},
 ImagePadding -> 50
 ]

 

 

 

 

Reference 
[1] https://mathematica.stackexchange.com/questions/627/1-plot-2-scale-axis

댓글


0%