4
$\begingroup$

I would like to use something like Point[x,y] to plot diamonds instead of small circles ("points"). I'm creating charts from scratch inside a Graphics expression using my own graphics functions. I've looked through the documentation and questions here and haven't found anything about how to do it.

$\endgroup$
  • $\begingroup$ Have you looked at this? $\endgroup$ – m_goldberg Aug 10 '14 at 16:39
  • $\begingroup$ I've seen your icon, but it hadn't registered as an example of what I wanted to do. Did you use translate? $\endgroup$ – George Wolfe Aug 10 '14 at 16:43
  • $\begingroup$ Sorry, I don't understand what you mean by "your icon". I gave a link to a previous posted question that I think relevant to yours. $\endgroup$ – m_goldberg Aug 10 '14 at 16:47
  • $\begingroup$ @m_goldberg I believe he meant your SE avatar which is automatically generated. It has some rhombi in its corners. $\endgroup$ – Jens Aug 10 '14 at 23:13
3
$\begingroup$

I use Translate for this purpose, which can not only translate along a single vector, but can create multiple copies translated along different vectors.

For example, let's use these (relative) positions:

points = RandomReal[10, {10, 2}]

Then

Graphics[
 Translate[Triangle[], points]
]

Mathematica graphics

Just make sure that your source object is centred around {0,0}, otherwise the plot will be misaligned, like here:

Graphics[
 {Translate[Triangle[], points],
  Red, PointSize[Large], Point[points]}
]

Mathematica graphics

I was just lazy to make a proper diamond so I use Triangle ...

$\endgroup$
  • $\begingroup$ Graphics[Translate[Rotate[Rectangle[], 45 Degree], points]] for diamonds? $\endgroup$ – kglr Aug 10 '14 at 16:19
  • $\begingroup$ @kguler That is not centred on {0,0}, so we get back to me being lazy to make a proper diamond ;-) $\endgroup$ – Szabolcs Aug 10 '14 at 16:22
  • $\begingroup$ @Szabolcs Yet another function I didn't know about. Translate seems pretty handy. It seems like it should work. $\endgroup$ – George Wolfe Aug 10 '14 at 16:38
  • $\begingroup$ @Szabolcs It works! Thanks. $\endgroup$ – George Wolfe Aug 10 '14 at 16:56
3
$\begingroup$

I would define a function diamond that draws diamonds centered at given point and with a specified bounding box.

diamond[xy : {x_, y_} : {0, 0}, wh : {w_, h_} : {1, 1}] := 
 Translate[Polygon[{{w/2., 0.}, {0., h/2.}, {-w/2., 0}, {0., -h/2.}}], xy]
Graphics[
  Table[{Hue[RandomReal[]], diamond[RandomReal[1, {2}], RandomReal[.2, {2}]]}, {200}]]

diamonds

$\endgroup$
  • $\begingroup$ How does the syntax [xy : {x_, y_} : {0, 0} work? Is it like a chain of defaults? $\endgroup$ – George Wolfe Aug 11 '14 at 0:22
  • 2
    $\begingroup$ @GeorgeWolfe. It does look like a chain of defaults, but it isn't. The full-form of the first argument to diamond is Optional[Pattern[xy, List[Pattern[x, Blank[]], Pattern[y, Blank[]]]], List[0, 0]], so it can be read (<pattern-name> : <pattern>) : <default>. Colon ( : ) is one of few operators in Mathematica that is interpreted differently according to context. The first colon is interpreted as the infix form of Pattern and the second as the infix form of Optional. $\endgroup$ – m_goldberg Aug 11 '14 at 2:56
2
$\begingroup$

here some examples how to define your own PlotMarkers

plotmarkers = 
 colors // 
  Map[Graphics[{#, Disk[]}, ImageSize -> 8] // 
      ToString[#, FormatType -> StandardForm] & // Function, #] &
ListPlot[{{1, 2, 3, 5, 8}, {2, 3, 6, 9, 10}, {4, 5, 7, 10, 12}}, 
 PlotMarkers -> plotmarkers, PlotRange -> All, 
 PlotRangeClipping -> False]

plotmarkers = {Style["\[FilledDiamond]", 16, Lighter@Red], 
  Style["\[FilledDiamond]", 16, Lighter@Blue], 
  Style["\[FilledDiamond]", 16, Lighter@Green]}
ListPlot[{{1, 2, 3, 5, 8}, {2, 3, 6, 9, 10}, {4, 5, 7, 10, 12}}, 
 PlotMarkers -> plotmarkers, PlotRange -> All, 
 PlotRangeClipping -> False]

You may use Inset or Text to place them accordingly in Graphics, or if available the Option PlotMarkers -> yourPlotmarkers

PlotMarkers

plotmarkers = {Style["\[FilledDiamond]", 16, Lighter@Red], 
  Style["\[FilledDiamond]", 16, Lighter@Blue], 
  Style["\[FilledDiamond]", 16, Lighter@Green]}
point[x_, y_, which_] := Inset[plotmarkers[[which]], {x, y}]
Graphics[{point[1, 0, 1], point[-1, 0, 1], point[0, 1, 2], 
  point[0, -1, 2], point[0, 0, 3]}]

diamond2

$\endgroup$
  • $\begingroup$ +1 Pity that you don't show that the green diamond is at (0,0) :) $\endgroup$ – eldo Aug 10 '14 at 17:15

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.