Python >> Tutoriel Python >  >> Python Tag >> Bokeh

suppression d'une ligne de la figure en bokeh

Il existe plusieurs façons :

# Keep the glyphs in a variable:
line2 = F.line('x', 'z', source=source, name='line2')

# or get the glyph from the Figure:
line2 = F.select_one({'name': 'line2'})

# in callback:
line2.visible = False

Cela fonctionnera pour maintenir une colonne de source de données 'x' partagée si les glyphes sont affectés en tant que variable et reçoivent un attribut de nom. La fonction remove remplit les colonnes 'y' appropriées avec nans, et la fonction restore remplace nans par les valeurs d'origine.

Les fonctions nécessitent des importations numpy et bokeh GlyphRenderer. Je ne suis pas sûr que cette méthode en vaille la peine étant donné la simple option marche/arrêt visible, mais je la poste quand même au cas où cela aiderait dans un autre cas d'utilisation.

Les glyphes à supprimer ou à restaurer sont référencés par des noms de glyphe, contenus dans une liste.

src_dict = source.data.copy()

def remove_glyphs(figure, glyph_name_list):
    renderers = figure.select(dict(type=GlyphRenderer))
    for r in renderers:
        if r.name in glyph_name_list:
            col = r.glyph.y
            r.data_source.data[col] = [np.nan] * len(r.data_source.data[col])

def restore_glyphs(figure, src_dict, glyph_name_list):
    renderers = figure.select(dict(type=GlyphRenderer))
    for r in renderers:
        if r.name in glyph_name_list:
            col = r.glyph.y
            r.data_source.data[col] = src_dict[col]

Exemple :

from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import Range1d, ColumnDataSource
from bokeh.models.renderers import GlyphRenderer

import numpy as np

output_notebook()

p = figure(plot_width=200, plot_height=150,
           x_range=Range1d(0, 6),
           y_range=Range1d(0, 10),
           toolbar_location=None)

source = ColumnDataSource(data=dict(x=[1, 3, 5],
                                    y1=[1, 1, 2],
                                    y2=[1, 2, 6],
                                    y3=[1, 3, 9]))

src_dict = source.data.copy()

line1 = p.line('x', 'y1',
               source=source,
               color='blue',
               name='g1',
               line_width=3)

line2 = p.line('x', 'y2',
               source=source,
               color='red',
               name='g2',
               line_width=3)

line3 = p.line('x', 'y3',
               source=source,
               color='green',
               name='g3',
               line_width=3)
print(source.data)
show(p)

sortie :

{'x': [1, 3, 5], 'y1': [1, 1, 2], 'y2': [1, 2, 6], 'y3': [1, 3, 9]}

remove_glyphs(p, ['g1', 'g2'])
print(source.data)
show(p)

sortie :

{'x': [1, 3, 5], 'y1': [nan, nan, nan], 'y2': [nan, nan, nan], 'y3': [1, 3, 9]}

restore_glyphs(p, src_dict, ['g1', 'g3'])
print(source.data)
show(p)

('g3' était déjà sur le tracé, et n'est pas affecté)

sortie :

{'x': [1, 3, 5], 'y1': [1, 1, 2], 'y2': [nan, nan, nan], 'y3': [1, 3, 9]}

restore_glyphs(p, src_dict, ['g2'])
print(source.data)
show(p)

sortie :

{'x': [1, 3, 5], 'y1': [1, 1, 2], 'y2': [1, 2, 6], 'y3': [1, 3, 9]}