3

I'm aware of this answer, Is it possible to control matplotlib marker orientation?, and the marker documentation, but is it possible to take an existing matplotlib marker symbol (nonregular polygon) and rotate it?

Specifically, I would like to rotate the thin diamond symbol ("d") 90 degrees, such that its long axis is horizontal.

LED
  • 129
  • 3
  • 6

3 Answers3

5

The marker "d" is a skewed version of a diamond, "D". You may create such diamond marker and skew it in the other direction.

For arbitrary angles, you may rotate the marker instead.

import matplotlib.pyplot as plt
from matplotlib.markers import MarkerStyle

fig, ax = plt.subplots()

plt.scatter([1,2,3],[1,2,3], s=225, marker="d")

m = MarkerStyle("D")
m._transform.scale(1.0, 0.6)

plt.scatter([1,2,3],[2,3,4], s=225, marker=m, color="crimson")

m = MarkerStyle("d")
m._transform.rotate_deg(60)

plt.scatter([1,2,3],[3,4,5], s=225, marker=m, color="limegreen")

plt.margins(0.5)
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • This is exactly what I am looking for... except that I want to use errorbar instead of scatter. Remarkably, it fails! The final error is `TypeError: float() argument must be a string or a number, not 'MarkerStyle'`. Anybody know a fix? – evanb Sep 28 '20 at 16:10
  • (I created a separate question, https://stackoverflow.com/questions/64106682/rotate-matplotlib-markers-with-errorbar-plot and opened https://github.com/matplotlib/matplotlib/issues/18600 to try and find a fix.) – evanb Sep 28 '20 at 17:39
0

It is possible to do this if you make a markerstyle class for a diamond and then modify its transform property yourself with a rotation, for whatever reason matplotlib didn't expose these class attributes for certain markers. I've included an example below.

import matplotlib as mpl
import matplotlib.pyplot as plt

# make a markerstyle class and modify the transform property by 90 degrees
t = mpl.markers.MarkerStyle(marker='d')
t._transform = t.get_transform().rotate_deg(90)
plt.scatter((0,1), (0,1), marker=t, s=100)

enter image description here

djakubosky
  • 907
  • 8
  • 15
-1

The rotation angel is given in 360-degree units:

x = [0,10,20,30,40,50,60,70,80,90]
for i in x:

    plt.plot(i, i, marker=(2, 0, -i), c='k',markersize=30, linestyle='None')
plt.margins(0.15); plt.grid();plt.show()

enter image description here

pyano
  • 1,885
  • 10
  • 28