2
$\begingroup$

This question already has an answer here:

I have two datasets on a ListPlot, but they have very different ranges. What I want to have is something like this.

enter image description here

I also want to know if it is possible to frame the plot legends and give a ubication on the plot just like in the figure.

$\endgroup$

marked as duplicate by Michael E2, xzczd, corey979, march, Henrik Schumacher Nov 17 '18 at 7:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1
$\begingroup$
dat1 = RandomReal[100, 100];
dat2 = RandomReal[2000, 100];
ip = {{30, 50}, {30, 25}};

plot1 = ListLinePlot[dat1, ImagePadding -> ip, ImageSize -> 500, 
PlotStyle -> Red, Frame -> {True, True, True, False}, 
FrameStyle -> {Black, Red, Black, Black}];

plot2 = ListLinePlot[dat2, ImageSize -> 500, PlotStyle -> Blue, 
ImagePadding -> ip, Frame -> {False, False, False, True}, 
FrameTicks -> {{False, All}, {False, False}}, FrameStyle -> Blue];

legend = SwatchLegend[{Red, Blue}, {"red", "blue"}]

Legended[Overlay[{plot1, plot2}], Placed[legend, Bottom]]

This is a simple application I know, which corrects a formatting issue with the axes. However, there are many similar questions with much more complete answers if you are looking for complexities and details.

If you want the legends to be in the charts, a crude solution is this.

dat1 = RandomReal[100, 100];
dat2 = RandomReal[2000, 100];
ip = {{30, 50}, {30, 25}};
plot1 = ListLinePlot[dat1, ImagePadding -> ip, ImageSize -> 500, 
PlotStyle -> Red, Frame -> {True, True, True, False}, 
FrameStyle -> {Black, Red, Black, Black}
, PlotLegends -> 
Placed[LineLegend[{"plot 1"}, LegendFunction -> Frame, 
  LegendMarkerSize -> {{55, 10}}], {0.8, 0.2}]];
plot2 = ListLinePlot[dat2, ImageSize -> 500, PlotStyle -> Blue, 
ImagePadding -> ip, Frame -> {False, False, False, True}, 
FrameTicks -> {{False, All}, {False, False}}, FrameStyle -> Blue
, PlotLegends -> 
Placed[LineLegend[{"plot 2"}, LegendFunction -> Frame, 
LegendMarkerSize -> {{55, 10}}], {0.8, 0.3}]];

Overlay[{plot1, plot2}]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.