3
$\begingroup$

I want to get such plot, using MeshFunctions

enter image description here

data = {{12, 2}, {5, 2}, {5, 4}, {14, 4}, {14, 1}, {1, 1}, {1, 5}, {18, 5}, {18, 0}};
ListLinePlot[data, PlotMarkers -> {Automatic, "Medium"}, MeshFunctions -> {#1 &}, Mesh -> 2, MeshStyle -> Opacity[0], 
MeshShading -> {Arrowheads[Medium]}] /. Line -> Arrow

gives arrowheads on horizontal lines:

enter image description here

ListLinePlot[data, PlotMarkers -> {Automatic, "Medium"},  MeshFunctions -> {#2 &}, Mesh -> 2, MeshStyle -> Opacity[0], 
  MeshShading -> {Arrowheads[Medium]}] /. Line -> Arrow

gives arrowheads on vertical lines:

enter image description here

But how to get arrowheads on both lines? Of course, I can combine two plots with Show. But is it possible to get one plot with arrowheads on vertical and horizontal lines? MeshFunctions -> {#1 &, #2 &} didn't help.

$\endgroup$

2 Answers 2

3
$\begingroup$

Instead of replacing a Line – which can go through multiple points – with a single Arrow, replace it with a sequence of multiple consecutive two-points arrows. Then you can tune the spacings with Mesh.

ListLinePlot[data, PlotMarkers -> {Automatic, "Medium"}, 
  MeshFunctions -> {#1 &, #2 &}, Mesh -> {2, 2}, 
  MeshStyle -> Opacity[0], 
  MeshShading -> {{arrowheads}, {arrowheads}}] /. 
 Line[pts_] :> (Arrow /@ Partition[pts, 2, 1])

Plot

$\endgroup$
3
$\begingroup$

To change every line into an arrow:

data = {{12, 2}, {5, 2}, {5, 4}, {14, 4}, {14, 1}, {1, 1}, {1, 
    5}, {18, 5}, {18, 0}};
ListLinePlot[data] /. Line[x__] :> Arrow[Partition[x, 2, 1]]

enter image description here

Or if you want several arrows on each line:

ListLinePlot[data] /. 
 Line[x__] :> {Arrow[Partition[x, 2, 1]], 
   Arrow[Partition[Riffle[#, Mean /@ Partition[#, 2, 1]] &[data], 2]]}

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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