5
$\begingroup$

By using Plot command I want to get one curve in form of circles and other like a solid line.

Plot[{Cos[x], Sin[x]}, {x, 0, 2 Pi},
      PlotStyle -> {Opacity[0]}, Mesh -> 30, MeshStyle -> {PointSize[Large], Blue}]

enter image description here

$\endgroup$

3 Answers 3

7
$\begingroup$

Edit: Michael E2 notes that style None should be used instead of Opacity[0] because the former avoids drawing the line while the latter simply makes it invisible. I have changed all styles in the code below accordingly.

There may be a syntax to use a different Mesh for each curve (I cannot recall) but if not you can always fall back to plotting separately and combining with Show:

Show[
 Plot[Cos[x], {x, 0, 2 Pi}, PlotStyle -> None, Mesh -> 30, 
   MeshStyle -> Directive[PointSize[Large], Blue]],
 Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> Red]
]

enter image description here

Two more ways to write the same thing:

Show[
 Plot[#[x], {x, 0, 2 Pi}, ##2] & @@@
  {{Cos, PlotStyle -> None, Mesh -> 30, 
    MeshStyle -> Directive[PointSize[Large], Blue]},
   {Sin, PlotStyle -> Red}}
]

Inner[
 Plot[#[x], {x, 0, 2 Pi}, #2] &,
 {Cos, Sin},
 {
  {PlotStyle -> None, Mesh -> 30, MeshStyle -> Directive[PointSize[Large], Blue]},
  PlotStyle -> Red
 },
 Show
]
$\endgroup$
1
  • $\begingroup$ It is nice to see how many questions that one has have been already asked and solved by other people! Mathematica syntax seems quite complex to me. I appreciate this website very much! $\endgroup$
    – Vicent
    Jul 1, 2014 at 12:58
1
$\begingroup$

[Works on V7-V10, Mac OSX.] Adapting my answer to the OP's other question, we can use PlotStyle to achieve the same effect with evenly spaced dots.

Plot[{Cos[x], Sin[x]},
 {x, 0, 2 Pi}, 
 PlotStyle -> {Directive[CapForm["Round"], Dashing[{0, 0.05}], Thickness[0.02]],
               Directive[]}]

Mathematica graphics

Caveat: The CapForm["Round"] directive does not seem to be honored in Windows (at least with respect to Dashing in PlotStyle), according to a comment to my answer to the linked question.

Side notes: The setting PlotStyle -> {Opacity[0]} in the OP's example code produces a curve, a Line object in fact, that cannot be seen. The setting PlotStyle -> None causes Plot not to produce a Line object at all.

I've never been able to figure out how to apply different mesh specifications to different functions using one Plot. I do it in the way Mr. Wizard has shown.

$\endgroup$
9
  • 1
    $\begingroup$ Unfortunately your caveat is true: this does not work in v7 under Windows, though CapForm["Round"] works elsewhere. $\endgroup$
    – Mr.Wizard
    Dec 26, 2013 at 14:39
  • 1
    $\begingroup$ @Mr.Wizard Oh, bother, as Pooh says. $\endgroup$
    – Michael E2
    Dec 26, 2013 at 14:44
  • $\begingroup$ Nice tip regarding PlotStyle -> None. $\endgroup$
    – Mr.Wizard
    Dec 26, 2013 at 14:46
  • $\begingroup$ "Oh, bother" I say that a lot too. :^) $\endgroup$
    – Mr.Wizard
    Dec 26, 2013 at 14:46
  • 1
    $\begingroup$ This is a perfect method; it's a shame that this has not been fixed in recent versions for Windows. I wonder if the back end graphics engine somehow doesn't support this. Do you know which APIs are used on your platform? $\endgroup$
    – Mr.Wizard
    Dec 26, 2013 at 14:51
0
$\begingroup$

MeshFunctions

You can use # Boole[#2 == foo[#]] & as the MeshFunctions setting to have the mesh points only on the curve of the function foo. For example:

Plot[{Cos[x], Sin[x]}, {x, 0, 2 Pi}, PlotPoints -> 300, 
 PlotStyle -> {None, Red},
 MeshFunctions -> {# Boole[#2 == Cos[#]] &}, Mesh -> 30, 
 MeshStyle -> Directive[PointSize[Large], Blue]]

enter image description here

Plot[{Cos[x], Sin[x], Sin[x] + Cos[x]}, {x, 0, 2 Pi}, PlotPoints -> 300,
 PlotStyle -> {None, Red, Green},
 MeshFunctions -> {# Boole[#2 == Cos[#]] &}, 
 Mesh -> 30, MeshStyle -> Directive[PointSize[Large], Blue]]

enter image description here

Plot[{Cos[x], Sin[x], Sin[x] + Cos[x]}, {x, 0, 2 Pi}, PlotPoints -> 300,
 PlotStyle -> {Blue, Red, None},
 MeshFunctions -> {# Boole[ #2 == Sin[#] + Cos[#]] &}, 
 Mesh -> 30, MeshStyle -> Directive[PointSize[Large], Green]]

enter image description here

Post-processing

ClearAll[postProcess, prims]
prims[x_, msh_, j_] := RotateRight[{{PointSize[Large], Point[Intersection[x, msh]]}, 
 Line[x], Line[x]}, j - 1]

postProcess[j_] :=  Module[{i = 1}, # /.  Block[{msh}, {Point[x_] :> (msh = x; {}), 
 Line[x_] :> (prims[x, msh, j][[i++]])}]] &;

plot = Plot[{Cos[x], Sin[x], Sin[x] + Cos[x]}, {x, 0, 2 Pi}, Mesh -> 30,
 PlotStyle -> {Blue, Red, Green}];

 postProcess[2]@plot

enter image description here

 postProcess[1]@plot

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.