1

我在使用 spanselector、cursor 和 fill_between 小部件时发现了一些小的图形问题,我想与您分享。

所有这些,都可以在这段代码中体验到(我取自 matplolib 示例)

"""
The SpanSelector is a mouse widget to select a xmin/xmax range and plot the
detail view of the selected region in the lower axes
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import SpanSelector
import matplotlib.widgets as widgets

Fig = plt.figure(figsize=(8,6))
Fig.set_facecolor('w')
Fig.set
Ax = Fig.add_subplot(211)


x = np.arange(0.0, 5.0, 0.01)
y = np.sin(2*np.pi*x) + 0.5*np.random.randn(len(x))

Ax.plot(x, y, '-')
Ax.set_ylim(-2,2)
Ax.set_title('Press left mouse button and drag to test')

RegionIndices = []

ax2 = Fig.add_subplot(212)
line2, = ax2.plot(x, y, '-')


def onselect(xmin, xmax):
    if len(RegionIndices) == 2:
        Ax.fill_between(x[:], 0.0, y[:],facecolor='White',alpha=1)
        del RegionIndices[:]


    indmin, indmax = np.searchsorted(x, (xmin, xmax))
    indmax = min(len(x)-1, indmax)

    Ax.fill_between(x[indmin:indmax], 0.0, y[indmin:indmax],facecolor='Blue',alpha=0.30)

    thisx = x[indmin:indmax]
    thisy = y[indmin:indmax]
    line2.set_data(thisx, thisy)
    ax2.set_xlim(thisx[0], thisx[-1])
    ax2.set_ylim(thisy.min(), thisy.max())
    Fig.canvas.draw()

    RegionIndices.append(xmin)
    RegionIndices.append(xmax)

# set useblit True on gtkagg for enhanced performance
span = SpanSelector(Ax, onselect, 'horizontal', useblit = True,rectprops=dict(alpha=0.5, facecolor='purple') )
cursor = widgets.Cursor(Ax, color="red", linewidth = 1, useblit = True)

plt.show()

我想知道是否有一些方法可以避免这两个小问题:

1)您可以看到,当您选择一个区域时,跨度选择器框(紫色)出现故障。在这段代码中,效果几乎不明显,但在多行的情节上非常烦人(我已经尝试了所有的 trueblit 组合都没有效果)

2)在此代码中,当您选择一个区域时,上图中线条和水平轴之间的区域被填充为蓝色。当您选择一个新区域时,旧区域填充为白色(以清除它),新区域再次填充为蓝色。但是,当我这样做时,绘制的线以及水平轴变得更粗......有没有办法清除这样的区域(由 fill_between 生成)而不会发生这种情况......或者是否有必要重新绘制图形?最初,我反对这样做,因为我有一个结构良好的代码,并且将所有数据再次导入 spanselector 方法似乎有点混乱......在 python 中删除选定区域的正确方法是什么?

任何建议都将受到欢迎

4

0 回答 0