Custom markers using Python (matplotlib)

倾然丶 夕夏残阳落幕 提交于 2019-12-23 00:50:18

问题


I would like to know how I can generate the marker for the black colored line shown in this picture. (Source: NCEP & NOAA) It's the marker for a storm or hurricane in standard weather maps.

I can probably generate an image file of the marker symbol. But, I am not aware of how I can tell matplotlib to use the image as a marker.


回答1:


The marker looks like a 6. If this is the case, you can use a 6 as a marker as follows:

import matplotlib.pyplot as plt

x = [1,2,3,4]
y = [2,3,1,4]

plt.scatter(x,y, s= 100,marker="$6$")

plt.show()

If this is not an option, you may define your custom marker using a path. To this end, the coordinates of the path need to be known. I have invented some values below, maybe they already suit the needs here.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as mpath

def get_hurricane():
    u = np.array([  [2.444,7.553],
                    [0.513,7.046],
                    [-1.243,5.433],
                    [-2.353,2.975],
                    [-2.578,0.092],
                    [-2.075,-1.795],
                    [-0.336,-2.870],
                    [2.609,-2.016]  ])
    u[:,0] -= 0.098
    codes = [1] + [2]*(len(u)-2) + [2] 
    u = np.append(u, -u[::-1], axis=0)
    codes += codes

    return mpath.Path(3*u, codes, closed=False)

hurricane = get_hurricane()
plt.scatter([1,1,2],[1.4,2.3,2.8], s=350, marker=hurricane, 
            edgecolors="crimson", facecolors='none', linewidth=2)
plt.scatter([0,1,2],[1,3,1], s=150, marker=hurricane, 
            edgecolors="k", facecolors='none')
plt.scatter([0,1.8,3],[0,2,4], s=150, marker="o", 
            edgecolors="k", facecolors='none')

plt.show()



来源:https://stackoverflow.com/questions/44726675/custom-markers-using-python-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!