matplotlib 创建自定义标记与填充广场和加上它里面

cvxl0en2  于 2023-10-24  发布在  其他
关注(0)|答案(2)|浏览(110)

我想创建一个自定义标记样式的线图。我特别想要一个带有加号或乘积符号的正方形(填充/未填充)。如何做到这一点。下面的代码不起作用

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np

# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create a figure and axis
fig, ax = plt.subplots()

# Define the custom marker as a combination of a filled square and a plus symbol
def custom_marker():
    square = mpatches.Rectangle((-0.5, -0.5), 1, 1, linewidth=0, edgecolor='none', facecolor='red', zorder=2)
    plus = plt.Line2D([0, 0], [-0.4, 0.4], color='white', linewidth=2, zorder=3)
    plus2 = plt.Line2D([-0.4, 0.4], [0, 0], color='white', linewidth=2, zorder=3)
    return [square, plus, plus2]

# Create a scatter plot with the custom marker style
ax.scatter(x, y, label="Data", marker=custom_marker()[0], c='blue', s=30)

# Customize the legend with the same custom marker
legend_marker = custom_marker()
ax.legend(handles=legend_marker, labels=["Custom Marker"])

# Display the plot
plt.show()
aoyhnmkz

aoyhnmkz1#

两次绘图怎么样?它似乎工作,你有更多的选择这样:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 6.28, 20)
y = np.sin(x)

plt.plot(
    x,
    y,
    linestyle='-',
    color='c',
    marker='s',
    markersize=8,
    markeredgewidth=1,
    markeredgecolor='b',
    markerfacecolor='orange',
)
plt.plot(
    x,
    y,
    linestyle=' ',
    color='c',
    marker='+',
    markersize=8,
    markeredgewidth=1,
    markeredgecolor='b',
    markerfacecolor='orange',
)
plt.show()

或者你可以用这个来表示没有任何肤色的正方形:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 6.28, 20)
y = np.sin(x)

plt.plot(
    x,
    y,
    linestyle='-',
    color='#00a2ff',
    marker='+',
    markersize=8,
    markeredgewidth=2,
    markeredgecolor='#bc00ca',
)
plt.plot(
    x,
    y,
    linestyle=' ',
    marker='s',
    markersize=12,
    markeredgewidth=1,
    markeredgecolor='b',
    markerfacecolor='none',
    #alpha=0.5,
)
plt.show()

或者给予 markerfacecolor 一个值,然后启用 alpha 以更改其不透明度。

t0ybt7op

t0ybt7op2#

你可以使用一个基于Path的自定义标记.
这样,传奇也就如预期的那样工作了。

import matplotlib.pyplot as plt
from matplotlib.path import Path

vertices=[[-15., -15.],
          [-15.,  15.],
          [ 15.,  15.],
          [ 15., -15.],
          [-15., -15.],
          [-13., -13.],
          [ 13., -13.],
          [ 13.,  13.],
          [-13.,  13.],
          [-13., -13.],
          [ -9.,  -1.],
          [ -1.,  -1.],
          [ -1.,  -9.],
          [  1.,  -9.],
          [  1.,  -1.],
          [  9.,  -1.],
          [  9.,   1.],
          [  1.,   1.],
          [  1.,   9.],
          [ -1.,   9.],
          [ -1.,   1.],
          [ -9.,   1.],
          [ -9.,  -1.]]

codes=[Path.MOVETO, 
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.MOVETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,]

p = Path(vertices=vertices, codes=codes)

fig, ax = plt.subplots()
ax.plot([1,2,3], [1,2,3], marker=p, ms=12, label="custom path marker")
plt.legend()

更新

也可以通过绘制一个带有“x”的矩形并使用edgecolor为“x”着色来填充标记(这也会为矩形添加边框......我不知道如何避免这种情况,而不单独绘制矩形和x)

import matplotlib.pyplot as plt
from matplotlib.path import Path

vertices=[[-16, -16],
          [-16,  16],
          [ 16,  16],
          [ 16, -16],
          [-16, -16],
          [  0,  10],
          [  0, -10],
          [-10,   0],
          [ 10,   0]]

codes=[Path.MOVETO, 
       Path.LINETO,
       Path.LINETO,
       Path.LINETO,
       Path.CLOSEPOLY,
       Path.MOVETO,
       Path.LINETO,
       Path.MOVETO,
       Path.LINETO
       ]       

p = Path(vertices=vertices, codes=codes)

fig, ax = plt.subplots()
ax.plot([1,2,3], [1,2,3], marker=p, ms=12, label="custom path marker",
        markerfacecolor="r", markeredgecolor="k", markeredgewidth=1.5)
plt.legend()

相关问题