我有一个关于matplotlib和"隐藏"lineplots的新问题。
我有一个wxFrame,它有一个matplotlib图和一个用来给出值的光标。效果非常好。在绘图中有多达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()
但这会导致每次更改时绘图颜色都会发生变化。我怎样才能让这些颜色保持不变?任何好的想法都会受到赞赏(坏的也会受到赞赏:)!
从与要绘制的行数相同长度的颜色图中创建颜色列表。您可以使用cm.<any_cmap>(np.linspace(0,1,number_of_lines))
执行此操作。
当您调用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()
这是一个糟糕的例子:
尝试将alpha=0
设置为有效地隐藏沿着self._axes.lines[mylineindex].set_alpha(0.0)
执行某些操作的行。通过这种方式,您应该只需要重新绘制那一行。