2016-08-22 2 views
1

Я читаю некоторые значения датчиков по последовательному порту.
Я хотел бы иметь возможность отображать/скрывать соответствующие строки в соответствии с нажатиями клавиш.Анимация Matplotlib: как сделать некоторые строки невидимыми

Код - это было слишком упрощено (извините, в любом случае, очень долго). Я хотел бы сделать ВСЕ синие линии в 3 подзаголовках показать/скрыть, нажав ; все красные линии показывают/скрывают, нажимая .

Я могу только «заморозить» линии, но не скрывать их.
Что мне не хватает?

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
from time import sleep 

# arrays of last 100 data (range 0.-1.) 
# read from serial port in a dedicated thread 
val1 = np.zeros(100)  
val2 = np.zeros(100)  

speed1=[] # speed of change of val1 and val2 
speed2=[] 

accel1=[] # speed of change of speed1 and speed2 
accel2=[] 

# initial level for each channel 
level1 = 0.2 
level2 = 0.8 

fig, ax = plt.subplots() 

ax = plt.subplot2grid((3,1),(0,0)) 
lineVal1, = ax.plot(np.zeros(100)) 
lineVal2, = ax.plot(np.zeros(100), color = "r") 
ax.set_ylim(-0.5, 1.5)  

axSpeed = plt.subplot2grid((3,1),(1,0)) 
lineSpeed1, = axSpeed.plot(np.zeros(99)) 
lineSpeed2, = axSpeed.plot(np.zeros(99), color = "r") 
axSpeed.set_ylim(-.1, +.1) 

axAccel = plt.subplot2grid((3,1),(2,0)) 
lineAccel1, = axAccel.plot(np.zeros(98)) 
lineAccel2, = axAccel.plot(np.zeros(98), color = "r") 
axAccel.set_ylim(-.1, +.1) 


showLines1 = True 
showLines2 = True 

def onKeyPress(event): 
    global showLines1, showLines2 
    if event.key == "1": showLines1 ^= True 
    if event.key == "2": showLines2 ^= True 



def updateData(): 
    global level1, level2 
    global val1, val2 
    global speed1, speed2 
    global accel1, accel2 

    clamp = lambda n, minn, maxn: max(min(maxn, n), minn) 

    # level1 and level2 are really read from serial port in a separate thread 
    # here we simulate them 
    level1 = clamp(level1 + (np.random.random()-.5)/20.0, 0.0, 1.0) 
    level2 = clamp(level2 + (np.random.random()-.5)/20.0, 0.0, 1.0) 


    # last reads are appended to data arrays 
    val1 = np.append(val1, level1)[-100:] 
    val2 = np.append(val2, level2)[-100:] 

    # as an example we calculate speed and acceleration on received data 
    speed1=val1[1:]-val1[:-1] 
    speed2=val2[1:]-val2[:-1] 

    accel1=speed1[1:]-speed1[:-1] 
    accel2=speed2[1:]-speed2[:-1] 

    yield 1 # FuncAnimation expects an iterator 



def visualize(i): 

    if showLines1: 
    lineVal1.set_ydata(val1) 
    lineSpeed1.set_ydata(speed1) 
    lineAccel1.set_ydata(accel1) 

    if showLines2: 
    lineVal2.set_ydata(val2) 
    lineSpeed2.set_ydata(speed2) 
    lineAccel2.set_ydata(accel2) 

    return lineVal1,lineVal2, lineSpeed1, lineSpeed2, lineAccel1, lineAccel2 

fig.canvas.mpl_connect('key_press_event', onKeyPress) 

ani = animation.FuncAnimation(fig, visualize, updateData, interval=50) 
plt.show() 
+0

Спасибо Ulf для редактирования моего поста. –

ответ

1

Вы должны скрыть/показать строки:

lineVal1.set_visible(showLines1) 
lineSpeed1.set_visible(showLines1) 
lineAccel1.set_visible(showLines1) 

lineVal2.set_visible(showLines2) 
lineSpeed2.set_visible(showLines2) 
lineAccel2.set_visible(showLines2) 
+0

Спасибо Тим! Я полностью пропустил это :-) –

Смежные вопросы