20
$\begingroup$

I would like to have a marker with white as its inner color, to be able to make this kind of graph:

enter image description here

If I use PlotMarkers -> Style["\[FilledSquare]", White], it changes both the inner color + border color but not the inner color only.

If I use PlotMarkers -> Style["\[EmptySquare]", White], the line of the curve goes above the marker.

How could I achieve that?

$\endgroup$
  • 4
    $\begingroup$ PlotMarkers->Graphics[...] as in here $\endgroup$ – Timothy Wofford Jul 29 '14 at 14:58
24
$\begingroup$

In Version 10 you can use the PlotTheme "OpenMarkersThick":

data = Table[{x, x^k}, {k, 1, 4}, {x, 0, 1, 0.1}]

ListLinePlot[data, PlotTheme -> {"OpenMarkersThick", "LargeLabels"}, 
 PlotLegends -> {x, x^2, x^3, x^4}] 

enter image description here

$\endgroup$
  • $\begingroup$ PlotTheme looks powerful. Will try it as soon as Mathematica 10 is downloaded! $\endgroup$ – Sulli Jul 29 '14 at 18:50
19
$\begingroup$

\[FilledSquare] is a font glyph and you cannot color parts of it.

I believe you need to draw your markers with Graphics primitives. For example:

square[in_, out_: Black, size_: 12] :=
 Graphics[{in, EdgeForm[{AbsoluteThickness[2], out}], Rectangle[]},
   PlotRangePadding -> 0,
   ImageSize -> size]

data = {{1, 2, 3, 5, 8}, {2, 3, 6, 9, 10}, {4, 5, 7, 10, 12}};

ListLinePlot[data, 
 PlotMarkers -> square /@ {Red, Green, Blue}
]

enter image description here

ListLinePlot[data,
  PlotMarkers -> square @@@ {{Yellow, Red}, {White, Blue}, {Black, Pink}}
]

enter image description here


Update: proof that this method can easily be used for markers of arbitrary shape.

Generic marker function:

marker[prim_, opts___][in_: White, out_: Black, size_: 13] :=
 Graphics[{in, EdgeForm[{AbsoluteThickness[2], Opacity[1], out}], prim},
  opts, ImageSize -> size]

Shapes:

square  = marker @ Rectangle[];
circle  = marker @ Disk[];
diamond = marker @ Polygon[{{0, 1}, {1, 2}, {2, 1}, {1, 0}}];
triangle =
 marker[
   Polygon[{{1, 0}, {0, Sqrt[3]}, {-1, 0}}], 
   AlignmentPoint -> {0, 1/Sqrt[3]}
 ];

Plot:

SeedRandom[12]

ListLinePlot[
  Accumulate /@ RandomReal[3, {4, 10}] {1, 2, 3, 4}, 
  PlotMarkers ->
    {square[], circle[Yellow], diamond[Brown, Pink], triangle[Magenta, Purple]},
  PlotLegends -> Automatic
]

enter image description here

$\endgroup$
  • $\begingroup$ @Öskå Go ahead and edit. I'll probably refactor to make it more terse, as is my style, but I'm out of time now. See you later. :-) $\endgroup$ – Mr.Wizard Jul 29 '14 at 15:12
  • 2
    $\begingroup$ @eldo You could use Primitives to produce any shape you like. Unlike other answers this one addresses the question in the Title: change the inner color of markers, not merely making centers white. (I wasn't aware of the theme "OpenMarkersThick" when I wrote this answer or surely would have included it.) I expect that chuy will earn the Accept but I don't think this is a bad answer. $\endgroup$ – Mr.Wizard Jul 29 '14 at 22:54
  • 1
    $\begingroup$ Your implementation of the triangle plot markers is incorrect as well as @eldo's: the center of the triangle is located on 1/3 of its height, not 1/2 as in your implementation. See here. $\endgroup$ – Alexey Popkov Jul 31 '14 at 12:23
  • 1
    $\begingroup$ Note that plot markers are aligned according to the AlignmentPoint option of Graphics. So if you add into your current triangle specification AlignmentPoint -> {0, 1/Sqrt[3]} you will get correct placement of the triangles. This value can be found via RegionCentroid@Polygon[{{1, 0}, {0, Sqrt[3]}, {-1, 0}}]. Of course for the other shapes in your answer this option should be different. $\endgroup$ – Alexey Popkov Jul 31 '14 at 15:12
  • 1
    $\begingroup$ @Alexey That is not what my code is doing. (Or at least not what I intend.) I changed the definition to marker[prim_, opts___] to pass options to Graphics. Sorry to disappoint. :-/ $\endgroup$ – Mr.Wizard Jul 31 '14 at 16:33
4
$\begingroup$

You could build your own PlotMarkers

ngon[p_, q_] := 
 Polygon[Table[{Cos[2 Pi k q/p], Sin[2 Pi k q/p]}, {k, p}]]

g1 = Graphics[{EdgeForm[Black], White, Disk[{0, 0}, 1]}];
g2 = Graphics[{EdgeForm[Black], White, Rectangle[{1, 1}]}];
g3 = Graphics[{EdgeForm[Black], White, ngon[4, 1]}];
g4 = Graphics[{EdgeForm[Black], White, Polygon[{{1, 0}, {0, Sqrt[3]}, {-1, 0}}]}];

ListLinePlot[Table[n^(1/p), {p, 4}, {n, 10}],
Filling -> Axis,
PlotLegends -> Automatic,
PlotMarkers -> Table[{s, 0.05}, {s, {g1, g2, g3, g4}}]]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Note that the triangle plot markers are placed incorrectly in this answer: the center of the triangle is located on 1/3 of its height, not 1/2. See here. $\endgroup$ – Alexey Popkov Jul 31 '14 at 12:36
2
$\begingroup$

ChartElementData["EmptyMarkers"]

ListLinePlot[4 Range[6] + RandomReal[{-1, 1}, {6, 20}], 
  Filling -> Thread[Range[5] -> List /@ Range[2, 6]],
  PlotMarkers -> Thread[{ChartElementData["EmptyMarkers"][[All, 1]], .05}], 
  PlotLegends -> Automatic] 

enter image description here

To get thicker edges, use

PlotMarkers -> Thread[{ChartElementData["EmptyMarkers"][[All, 1]] /. 
    Thickness[_] :> AbsoluteThickness[3], .05}]

enter image description here

$\endgroup$
  • $\begingroup$ Note that the triangle markers with this approach will be positioned incorrectly: try ListLinePlot[Table[{i,j},{i,6},{j,6}],PlotMarkers->Thread[{ChartElementData["EmptyMarkers"][[All,1]],.1}],GridLines->{None,All}]/.p_Polygon:>{Blue,PointSize[.15],Point[RegionCentroid[p]]} to see this. Detailed explanations are here. $\endgroup$ – Alexey Popkov Jan 16 '18 at 6:58
  • $\begingroup$ @AlexeyPopkov, you are right. It is easier to see that the triangle marker is not centered at {.5, .5} is to evaluate ChartElementData["EmptyMarkers"][[4, 1]] /. Opacity[0] -> Opacity[.5] // Show[#, Frame -> True, GridLines -> {{.5}, {.5}}, GridLinesStyle -> Red, Method -> {"GridLinesInFront" -> True}] & $\endgroup$ – kglr Jan 16 '18 at 7:16
  • $\begingroup$ Actually as I explain here the triangle should NOT be centered at {.5, .5}, but at RegionCentro‌​id at {.5, 1/3} (for the upper triangle). $\endgroup$ – Alexey Popkov Jan 16 '18 at 7:20
  • $\begingroup$ @Alexey, yes,;actually I meant to say it is centered at {.5,.5} . $\endgroup$ – kglr Jan 16 '18 at 7:27

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.