7
$\begingroup$

I would like to plot a continuous function with Plot with plot markers as if it were a ListPlot plot.

Simple example: I have

f[x_]:=x
Plot[f[x],{x,0,10}]

but for external reasons I need the figure to look like that of

ListPlot[Table[{x,f[x]},{x,0,10}]]

It works well. But imagine now my f[x] function contains several functions:

f[x_]:={x,x^2,x^3}

Plotting with plot is the same:

Plot[f[x],{x,0,10}]

However, the conversion to a set of points with Table is now complicated.

The question is: what would be the easiest way (the shortest code) to produce a plot marker plot for such f[x]? Can it be done directly with Plot without using ListPlot and Table ?

$\endgroup$
0

4 Answers 4

7
$\begingroup$

Perhaps this would help:

f[x_]:={x,x^2,x^3};
DiscretePlot[Evaluate[f[x]], {x, 0, 1, 0.1}, Filling -> None, 
 PlotMarkers -> {{"a", 5}, {"b", 10}, {"c", 15}}, 
 PlotLegends -> "Expressions", Frame -> True]

enter image description here

$\endgroup$
3
  • 3
    $\begingroup$ Odd that PlotLegend does not print the symbols defined by PlotMarkers? $\endgroup$ Mar 21, 2014 at 12:15
  • $\begingroup$ @bobthechemist I could have made it do that but it is not default and just wanted to illustrate a way to 'discretize' as per q...legends always need some fiddling $\endgroup$
    – ubpdqn
    Mar 21, 2014 at 12:18
  • $\begingroup$ Thank you. Choosing this solution as the most natural one, although two others are also good. $\endgroup$
    – Dr_Zaszuś
    Mar 31, 2014 at 13:54
4
$\begingroup$

If you want to avoid using ListPlot all together, you can explore the Mesh option to Plot:

Plot[f[x], {x, 0, 1}, Mesh -> 20, MeshShading -> {None}]

Mathematica graphics

The same MeshStyle will be applied to each of the functions, making this solution somewhat limited. If you insist on a Plot solution, however, we can do something silly like this:

Show@{Plot[#[[1]], {x, 0, 1}, Mesh -> 20, MeshShading -> {None}, 
     MeshStyle -> #[[2]]] & /@ Transpose[{f[x], {Red, Green, Blue}}]}

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ Which is which? ;) $\endgroup$
    – Kuba
    Mar 21, 2014 at 13:21
  • $\begingroup$ @Kuba Yeah, limitation to this approach. $\endgroup$ Mar 21, 2014 at 13:22
  • $\begingroup$ @kuba how's that :-) $\endgroup$ Mar 21, 2014 at 13:26
  • $\begingroup$ Works :) but can't compete for the shortest solution :P $\endgroup$
    – Kuba
    Mar 21, 2014 at 13:32
  • $\begingroup$ @Kuba, hence the reason you'll never see me on code golf $\endgroup$ Mar 21, 2014 at 13:34
3
$\begingroup$

The DiscretePlot proposed by ubpdqn is a natural solution. You can, however, expand the discretization like the one you used ListPlot[Table[{x,f[x]},{x,0,10}]] on the list like this f[x_]:={x,x^2,x^3}. Indeed, this is your function:

f[x_] := {x, x^2, x^3};

Let us define a function making a list like the one you used, but a bit differently:

g[z_] := Table[{x, z}, {x, 0, 1, 0.1}];

Then the solution is

ListPlot[g /@ f[x], PlotMarkers -> Automatic]

The result should look like this: enter image description here

$\endgroup$
1
$\begingroup$

Update 2: Post-processing to replace lines with markers works in both version 9 and version 11:

Module[{i = 1, j}, Plot[Evaluate[funcs], {t, 0, 2 Pi}, PlotPoints -> 40, 
  MaxRecursion -> 0, PlotStyle -> {Red, Green, Blue}, 
  PlotLegends -> LineLegend["Expressions", Joined -> False, LegendMarkers -> markers]] /. 
   Line[x_] :> (j = i++; (Inset[markers[[j]], #] & /@ x))]

enter image description here

Update: It turns out that, in version 9, the same trick works with PlotStyle:

Plot[Evaluate[funcs], {t, 0, 2 Pi}, PlotPoints -> 50, MaxRecursion -> 0, 
 PlotStyle -> (Table[With[{i = i}, 
     Function[w, Map[Function[z, Inset[i, z]], w]] @@ ## &], {i, markers}]), 
 PlotLegends -> LineLegend["Expressions", Joined -> False, LegendMarkers -> markers]]

enter image description here

Although much cleaner than the MeshStyle trick in the original answer, unfortunately, this doesn't work in version 11 whereas MeshStyle trick works in both version 9 and 11.

Original answer:

You can use Mesh and inject the plot markers into the MeshStyle setting as follows:

funcs = {t Sin[t] Cos[t], t Sinc[t], Cos[t] Sinc[t] 2 t};
mesh = {20, 20, 30};
colors = ColorData[1, "ColorList"][[;; 3]];
markers = {"A", "B", "\[FilledUpTriangle]"};

Show[Module[{ins = Style[#4, #3, 16]}, Plot[#, {t, 0, 2 Pi}, Mesh -> #2, PlotStyle -> #3, 
     MeshStyle -> (Function[w, Map[Function[z, Inset[ins, z]], w]] @@ ## &), 
     PlotLegends -> Row[{ins, #}, Spacer[5]]]] & @@@ 
  Transpose[{funcs, mesh, colors, markers}]]

enter image description here

Change PlotStyle -> #3 to PlotStyle -> None to remove the lines: enter image description here

$\endgroup$

Your Answer

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

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