TransWikia.com

How to change DataRange independently for various lists in same plot

Mathematica Asked by Rossana on January 19, 2021

I am plotting three lists of different size on the same plot. I want the plot to start from zero rather than 1 on the x-axis. In the past, I have used DataRange->{0,Max} to achieve this, but in this case the data range of all lists is changed, which is not what I want.

For example, consider:

a = {1, 2, 3};
b = {2, 3, 4, 5, 6};
c = {3, 4, 5, 6, 7, 8, 9, 10};

ListLogPlot[{a, b, c}, Joined -> True, PlotLegends -> {"a", "b", "c"}]

which yields:

If I add in the DataRange requirement,
ListLogPlot[{a, b, c}, Joined -> True, PlotMarkers -> {"a", "b", "c"}, PlotLegends -> {"a", "b", "c"}, DataRange -> {0, 7}],
I get the following plot, where all my lists are stretched to span the range 0->7:

I want each list to start at zero, but preserve its range (so 0->2 for a, 0-> 4 for b, etc. ). I am plotting them all together rather than creating different plots because I need the legend.

2 Answers

You can use TemporalData with time spec Automatic to get uniformly spaced times starting at 0:

td = TemporalData[{a, b, c}, Automatic];

ListLogPlot[td, Joined -> True, 
  PlotLegends -> {"a", "b", "c"}, 
  PlotMarkers -> (Style[#, 14, Background -> White] & /@ {"a", "b", "c"})]

enter image description here

Use TimeSeriesRescale to modify time ranges:

ListLogPlot[TimeSeriesRescale[td, {10, 12}] , 
 Joined -> True, PlotLegends -> {"a", "b", "c"},
 PlotMarkers -> (Style[#, 14, Background -> White] & /@ {"a", "b", "c"})]

enter image description here

Correct answer by kglr on January 19, 2021

a = {1, 2, 3};
b = {2, 3, 4, 5, 6};
c = {3, 4, 5, 6, 7, 8, 9, 10}; 
ListLogPlot[
 Table[MapIndexed[{First[#2] - 1, #1} &, x], {x, {a, b, c}}], 
 Joined -> True, DataRange -> {0, 7}, PlotMarkers -> {"a", "b", "c"}, 
 PlotLegends -> {"a", "b", "c"}]

enter image description here

Answered by Jean-Pierre on January 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP