0
$\begingroup$

With the following data, I intend to produce 5 lines:

data = {{{0.414513, 0.0489003, 0.397241, 0.0357143}, {0.40829, 0.0422302, 
   0.417166, 0.0365926}, {0.384615, 0.0833333, 0.401338, 
   0.0826405}, {0.384615, 0.0833333, 0.401338, 0.0826405}, {0.392318, 
   0.10824, 0.392318, 0.10824}}}

The first number (0.414513) is the x1-coordinate, the second number (0.0489003) is the y1-coordinate. The numbers 0.397241 and 0.0357143 are the x2 and y2 coordinates, thus allowing the plotting of the first line. Each row thus contains information for one particular line.

My question is simply, how do I plot the five lines at once using simple code?

$\endgroup$
1
  • $\begingroup$ The last one of your "lines" starts and ends at the same point. Is that correct? $\endgroup$
    – MarcoB
    Mar 16 at 19:28
3
$\begingroup$

Mathematica is expecting pairs of coordinates in order to plot. A single line should be in the form line1 = {{x1, y1}, {x2, y2}}, with a series of lines having the form {line1, line2, ...}. We can use ArrayReshape to put your data in the correct form.

data = {{{0.414513, 0.0489003, 0.397241, 0.0357143}, {0.40829, 
    0.0422302, 0.417166, 0.0365926}, {0.384615, 0.0833333, 0.401338, 
    0.0826405}, {0.384615, 0.0833333, 0.401338, 0.0826405}, {0.392318,
     0.10824, 0.392318, 0.10824}}}
lines = ArrayReshape[data, {Length@data[[1]], 2, 2}]
ListLinePlot[lines]

A plot of the lines.

Only 4 lines are shown because the 3rd and 4th have almost the same coordinates and the 3rd (which would be green) is covered up by the 4th (orange). You could change the plot style to make the lines dashed or partially transparent if you want to see both.

EDIT 01

In order to make all the lines the same colour, you just need to give PlotStyle a single colour to work with. If you'd like the lines to be arrows, you could use Graphics instead of ListLinePlot like this:

data = {{{0.414513, 0.0489003, 0.397241, 0.0357143}, {0.40829, 
0.0422302, 0.417166, 0.0365926}, {0.384615, 0.0833333, 0.401338, 
0.0826405}, {0.384615, 0.0833333, 0.401338, 0.0826405}, {0.392318,
 0.10824, 0.392318, 0.10824}}}
lines = ArrayReshape[data, {Length@data[[1]], 2, 2}]
Graphics[{
    Red,
    Arrow /@ lines
  },
  AspectRatio -> 1/GoldenRatio,
  Axes -> True,
  AxesOrigin -> {0.384, 0}
]

Depending on what you're planning to do, sometimes just starting from Graphics is easier. Ultimately, Mathematica turns plots into Graphics anyways. However, there's another way that is a little more concise, and that's to create the plot with ListLinePlot, and then replace all lines with arrows.

data = {{{0.414513, 0.0489003, 0.397241, 0.0357143}, {0.40829, 
    0.0422302, 0.417166, 0.0365926}, {0.384615, 0.0833333, 0.401338, 
    0.0826405}, {0.384615, 0.0833333, 0.401338, 0.0826405}, {0.392318,
     0.10824, 0.392318, 0.10824}}}
lines = ArrayReshape[data, {Length@data[[1]], 2, 2}]
plt = ListLinePlot[lines]/.Line -> Arrow

Plot with arrows instead of lines.

$\endgroup$
3
  • $\begingroup$ Is there an easy way to make the lines arrows, such that they show direction of movement? $\endgroup$
    – user120911
    Mar 16 at 19:26
  • $\begingroup$ @user120911 I edited my post. Is that something like what you're looking for? $\endgroup$
    – MassDefect
    Mar 16 at 19:28
  • 1
    $\begingroup$ An alternative to reshaping could be Apply[Arrow[{{#1, #2}, {#3, #4}}] &, data, {2}] $\endgroup$
    – MarcoB
    Mar 16 at 19:29

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.