2
$\begingroup$

I have imported as a csv file and converted to a Dataset. The data is in the following csv format

A(1),B(1),A(2),B(2),A(3),B(3),A(4),B(4),A(5),B(5)
1,2,4,5,6,4,7,8,9,0
5,3,2,5,5,2,3,1,8,3
7,3,8,3,2,2,3,4,5,6
9,8,7,2,5,3,6,7,8,9

Now I imported the data using

ds = Import["data.csv", "Dataset", HeaderLines -> 1]

Now basically I want to plot $B(i)$ v/s $A(i)$ for different $i$'s on the same plot. I can do that for 1 $i$ using

ds[ListLinePlot, {"A(1)", "B(1)"}]

And I get the following curve enter image description here

Now, I want to do the same for all $i$'s and plot them on the same plot. I know I can probably just write them each down but my original dataset (not shown here) is quite large. So I want to automate it for all $i$. Any easy way to execute such a command?

$\endgroup$
3
$\begingroup$

Consider the following:

data = ImportString[
         "A(1),B(1),A(2),B(2),A(3),B(3),A(4),B(4),A(5),B(5)
          1,2,4,5,6,4,7,8,9,0
          5,3,2,5,5,2,3,1,8,3
          7,3,8,3,2,2,3,4,5,6
          9,8,7,2,5,3,6,7,8,9", HeaderLines -> 1
       ];

ListCurvePathPlot[
  Transpose /@ Partition[Transpose[data], 2],
  PlotLegends -> Range[5]
]

enter image description here

$\endgroup$
4
  • $\begingroup$ Is there any other way to do it without partition? Let's say I want to run a loop over i, because in my original dataset there are some other values such as C(i) in the dataset as well. $\endgroup$ – Indeterminate Feb 8 at 2:44
  • $\begingroup$ @Indeterminate Explicit loops are almost never the answer in MMA. The Partition approach would work without change for however main pairs of values you have in your dataset, there's no need to change it. If it doesn't work when you try, please show the structure of the actual dataset you have. $\endgroup$ – MarcoB Feb 8 at 4:01
  • $\begingroup$ Here is my original dataset and the problem I am trying to solve: mathematica.stackexchange.com/questions/239583/… $\endgroup$ – Indeterminate Feb 8 at 4:02
  • $\begingroup$ @Indeterminate OK I see. I think you may have oversimplified here a bit. I've added an answer to that question. $\endgroup$ – MarcoB Feb 8 at 4:10
2
$\begingroup$

Here is another possible way if you want to stay within the dataset framework.

ds = SemanticImportString[
   "A(1),B(1),A(2),B(2),A(3),B(3),A(4),B(4),A(5),B(5)
1,2,4,5,6,4,7,8,9,0
5,3,2,5,5,2,3,1,8,3
7,3,8,3,2,2,3,4,5,6
9,8,7,2,5,3,6,7,8,9"];
(*Create list of B versus A pairs*)
xypairs = Partition[ds // First // Keys // Normal, 2];
(*Standard ListLinePlot*)
dsllp = ds[Transpose /* Values /* ListLinePlot, xypairs]
(* Marco's ListCurvePathPlot *)
dslcpp = ds[Transpose /* Values /* ListCurvePathPlot, xypairs]

No legend plots

I did not find a way to add legends directly, so I had to manually add them as shown below:

(*Use standard color list 97*)
colors = ColorData[97, "ColorList"][[;; Length@xypairs]];
labels = Style[StringTemplate["`2` vs `1`"][##], 14] & @@@ xypairs;
legend = LineLegend[Directive[#, AbsoluteThickness[2]] & /@ colors, 
   labels];
Legended[dsllp, Placed[legend, Automatic]]
Legended[dslcpp, Placed[legend, Automatic]]

Plots with legends

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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