从Matplotlib Plot隐藏线而不重绘它们?

时间:2015-11-09 16:07:06

标签: python matplotlib

我在matplotlib上遇到了一个新问题,并且隐藏了' lineplots。

我有一个带有matplotlib图的wxFrame和一个给出值的游标。工作得很好。 在情节中最多有13行,我想用复选框显示和隐藏它们,这也很好。

这是我的代码'重绘'

def Draw(self, visibility = None):
    self._axes.lines = []
    # if no visibility data, draw all
    if visibility is None:
        visibility = []
        for i in range(len(self._data)):
            visibility.append(True)
    else:
        # check if length match
        if len(self._data) != len(visibility):
            raise AttributeError('Visibility list length does not match plot count')
    # draw the lines you want
    for i in range(len(self._data)):
        if visibility[i]:
            plotName = 'FFD ' + str(i + 1)
            self._axes.plot(self.timebase, self._data[i], picker=5, label = plotName)
    #if there are any lines, draw a legend
    if len(self._axes.lines):
        self._axes.legend(prop={'size':9})

    #update the canvas
    self._canvas.draw()

但是这会导致每个变化的情节颜色发生变化。我怎样才能保留颜色?任何好的想法都值得赞赏(坏的也赞赏:))!

2 个答案:

答案 0 :(得分:0)

这是一个糟糕的:

尝试设置alpha=0以有效隐藏行self._axes.lines[mylineindex].set_alpha(0.0)行。通过这种方式,您只需要重绘一行。

答案 1 :(得分:0)

从色彩映射创建一个颜色列表,其长度与要绘制的行数相同。您可以使用plot

执行此操作

当您致电color=colours[i]设置import numpy as np import matplotlib.cm as cm def Draw(self, visibility = None): nlines = len(self._data) # Choose whichever colormap you like here instead of jet colours = cm.jet(np.linspace(0,1,nlines)) # a list of colours the same length as your data self._axes.lines = [] # if no visibility data, draw all if visibility is None: visibility = [] for i in range(len(self._data)): visibility.append(True) else: # check if length match if len(self._data) != len(visibility): raise AttributeError('Visibility list length does not match plot count') # draw the lines you want for i in range(len(self._data)): if visibility[i]: plotName = 'FFD ' + str(i + 1) # pick a colour from the colour list using the color kwarg self._axes.plot(self.timebase, self._data[i], picker=5, color=colours[i], label = plotName) #if there are any lines, draw a legend if len(self._axes.lines): self._axes.legend(prop={'size':9}) #update the canvas self._canvas.draw() 时。这样,每个绘图将始终被赋予相同的颜色,即使没有绘制,其他行也会被分配。颜色不会受到影响。

<input>