是否可以控制matplotlib标记方向?

pha*_*ann 18 python matplotlib matplotlib-basemap

我想知道我是否有三角形标记,是否可以控制其方向?我有一系列的facets及其相应的顶点,我想绘制它们的底图.我知道使用Mayavi和tvtk.PolyData时它是直接的脚本.但是因为我正在处理地图而不是3D对象,所以事情变得有点复杂.

ps:对于地图我正在使用底图工具.

我感谢你的帮助.

Ffi*_*ydd 26

您可以使用关键字参数创建自定义多边形marker,并将其传递给3个数字的元组(number of sides, style, rotation).

要创建您将使用的三角形(3, 0, rotation),下面显示了一个示例.

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()
Run Code Online (Sandbox Code Playgroud)

情节

  • 击败我-并举一个例子!(+1) (2认同)
  • 三角形不能是等边吗?我可以传递一个 Nx3 形状的元组,而不是迭代每个方面吗? (2认同)

dja*_*sky 6

我只想添加一种旋转其他非常规多边形标记样式的方法。下面,通过修改标记样式类的transform属性,旋转了“细菱形”,“ plus”和“ vline”。

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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Mar*_*hke 6

自定义解决方案matplotlib.path.Path(不规则三角形)

如果您寻找一个标记符号,您可以在其中明显地从 [0, 2pi) 拒绝方向,则可以从路径构造一个标记。由于路径由绘图例程自动缩放(使得最外点接触框 -1 <= x, y <= 1),因此您需要额外的点大小缩放。

在此输入图像描述

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()
Run Code Online (Sandbox Code Playgroud)


mgi*_*son 5

看一下matplotlib.markers模块:

(numsides, 0, angle)- 一个有边的正多边形numsides,旋转angle
(numsides, 1, angle)- 带有边的星形符号numsides,旋转angle
(numsides, 2, angle)- 有边的星号numsides,旋转angle

例如,您可以使用具有指定角度的任意多边形:

marker = (3, 0, 45)  # triangle rotated by 45 degrees.
Run Code Online (Sandbox Code Playgroud)