31

If I have a triangular marker, is it possible to control its orientation? I have a series of facets, with their corresponding vertices, and I would like to plot a basemap of them. I know it is straightforward script when using Mayavi and tvtk.PolyData. But since I'm dealing with maps and not 3D objects, things got a bit complicated.

ps: for maps I'm using basemap tool.

tdy
  • 36,675
  • 19
  • 86
  • 83
phasselmann
  • 465
  • 2
  • 6
  • 17

5 Answers5

45

You can create custom polygons using the keyword argument marker and passing it a tuple of 3 numbers (number of sides, style, rotation).

To create a triangle you would use (3, 0, rotation), an example is shown below.

import matplotlib.pyplot as plt

x = [1,2,3]
for i in x:
    plt.plot(i, i, marker=(3, 0, i*90), markersize=20, linestyle='None')

plt.xlim([0,4])
plt.ylim([0,4])

plt.show()

Plot

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • 3
    Beat me to it -- and with an example too! (+1) – mgilson Apr 28 '14 at 15:56
  • 2
    Could the triangles not be equilateral? And could I pass a Nx3-shape tuple, instead iterating over each facet? – phasselmann Apr 28 '14 at 16:00
  • The triangles have to be equilateral with this method (it creates a regular polygon so if you passed it 4 it would create a square). I don't think matplotlib is able to create more advanced symbols than that by itself. If you could generate the symbols you want yourself as images you could possibly try [this](http://stackoverflow.com/questions/2318288/how-to-use-custom-marker-with-plot) – Ffisegydd Apr 28 '14 at 16:07
  • How could I make the 3rd element i.e rotation - a dataframe series. Im plotting in terms of , x=df1['A'], y=df1['Speed'] z=df1['Direction'] – SLE Jul 20 '16 at 11:05
  • @SLE off the top of my head, it's be exactly the same really. You'd have to iterate row-wise over the dataframe and do `plt.plot(df['A'], df['Speed'], marker = (...some calculation that you want that uses df['Direction']...))`. Not very efficient (iterating and plotting N single points like that) though. If you have M directions that are shared with other rows (N, S, E, W as an example) then you could index the df by direction and then plot those indexed parts, then you'd just have M different `plt.plot` calls for the M different directions. – Ffisegydd Jul 20 '16 at 12:51
13

I just wanted to add a method to rotate other non-regular polygon marker styles. Below I have rotated the "thin diamond" and "plus" and "vline" by modifying the transform attribute of the marker style class.

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

for m in ['d', '+', '|']:

    for i in range(5):
        a1, a2  = np.random.random(2)
        angle = np.random.choice([180, 45, 90, 35])

        # make a markerstyle class instance and modify its transform prop
        t = mpl.markers.MarkerStyle(marker=m)
        t._transform = t.get_transform().rotate_deg(angle)
        plt.scatter((a1), (a2), marker=t, s=100)

enter image description here

djakubosky
  • 907
  • 8
  • 15
  • I noticed that `plt.plot` does not accept MarkerStyle objects, it throws an error: `TypeError: float() argument must be a string or a number, not 'MarkerStyle'`. `plt.scatter`works fine though. – Henri Oct 22 '20 at 14:19
4

Have a look at the matplotlib.markers module. Of particular interest is the fact that you can use an arbitrary polygon with a specified angle:

marker = (3, 0, 45)  # triangle rotated by 45 degrees.
mgilson
  • 300,191
  • 65
  • 633
  • 696
4

solution with custom matplotlib.path.Path (irregular triangle)

If you look for a marker symbol, where you can clearly decline the orientation from [0, 2pi), you can construct a marker from a path. As paths are automatically scaled by the plotting routine (such that the most outer point touches the box -1 <= x, y <= 1), you need additional point size scaling.

enter image description here

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt



def gen_arrow_head_marker(rot):
    """generate a marker to plot with matplotlib scatter, plot, ...

    https://matplotlib.org/stable/api/markers_api.html#module-matplotlib.markers

    rot=0: positive x direction
    Parameters
    ----------
    rot : float
        rotation in degree
        0 is positive x direction

    Returns
    -------
    arrow_head_marker : Path
        use this path for marker argument of plt.scatter
    scale : float
        multiply a argument of plt.scatter with this factor got get markers
        with the same size independent of their rotation.
        Paths are autoscaled to a box of size -1 <= x, y <= 1 by plt.scatter
    """
    arr = np.array([[.1, .3], [.1, -.3], [1, 0], [.1, .3]])  # arrow shape
    angle = rot / 180 * np.pi
    rot_mat = np.array([
        [np.cos(angle), np.sin(angle)],
        [-np.sin(angle), np.cos(angle)]
        ])
    arr = np.matmul(arr, rot_mat)  # rotates the arrow

    # scale
    x0 = np.amin(arr[:, 0])
    x1 = np.amax(arr[:, 0])
    y0 = np.amin(arr[:, 1])
    y1 = np.amax(arr[:, 1])
    scale = np.amax(np.abs([x0, x1, y0, y1]))
    codes = [mpl.path.Path.MOVETO, mpl.path.Path.LINETO,mpl.path.Path.LINETO, mpl.path.Path.CLOSEPOLY]
    arrow_head_marker = mpl.path.Path(arr, codes)
    return arrow_head_marker, scale

fig, ax = plt.subplots()
for rot in [0, 15, 30, 45, 60, 90, 110, 180, 210, 315, 360]:

    marker, scale = gen_arrow_head_marker(rot)
    markersize = 25
    ax.scatter(rot, 0, marker=marker, s=(markersize*scale)**2)

ax.set_xlabel('Rotation in degree')

plt.show()
Markus Dutschke
  • 9,341
  • 4
  • 63
  • 58
2

I think there's a better and more comprehensive answer as for Matplotlib 3.3.3 :

There is an option "verts" for specifiyng the marker, which has the form of a list of (x,y) tuples, these being the vertices of your path. This allows you to draw a marker of virtually any shape, filled or not, open or closed etc. As far as I've tested, the other marker options (see below) are still applicable.

For example

   plt.plot(x,y,
       marker=[(0,-24),(-10,-20),(10,-16),(-10,12),(10,8),(0,-4),(0,0)],
       markersize=42, color='w', linestyle='None',
       markeredgecolor='k', markeredgewidth= 2.)

will create a marker with the shape of a spring. The size is automatically mapped to a unit square, and your point (0,0) will be placed at x,y. From here, making a function to rotate a given angle the whole list of coordinates should be a trivial task.

custom, spring-shaped, marker

JC Cheloven
  • 139
  • 2
  • 6