如何孵化PolyCollection实例?

3 投票
1 回答
529 浏览
提问于 2025-04-17 09:03

有没有办法让PolyCollection实例“孵化”出来?我想要从fill_betweenx函数中得到一个PolyCollection。

import matplotlib.mlab as mlab
from matplotlib.pyplot import figure, show
import numpy as np

x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2*np.pi*x)
y2 = 1.2*np.sin(4*np.pi*x)

fig = figure()
ax1 = fig.add_subplot(111)

PC = ax1.fill_betweenx(x, 0, y1)
# I want to do something like this
# PC.set_hatch('\')
# but there is no such method

show()

1 个回答

2

这有点像小技巧,但你应该可以这样做:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import PathPatch

x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2*np.pi*x)
y2 = 1.2*np.sin(4*np.pi*x)

fig, ax = plt.subplots()
pc = ax.fill_betweenx(x, 0, y1, color='blue')

# Now we'll add the hatches...
for path in pc.get_paths():
    patch = PathPatch(path, hatch='/', facecolor='none')
    ax.add_patch(patch)

plt.show()

在这里输入图片描述

撰写回答