2015-03-27 8 views
23

Creo una figura in una funzione, ad es.Come posso ottenere `set_xlim` o` set_ylim` in Bokeh?

import numpy 
from bokeh.plotting import figure, show, output_notebook 
output_notebook() 

def make_fig(): 
    rows = cols = 16 
    img = numpy.ones((rows, cols), dtype=numpy.uint32) 
    view = img.view(dtype=numpy.uint8).reshape((rows, cols, 4)) 
    view[:, :, 0] = numpy.arange(256) 
    view[:, :, 1] = 265 - numpy.arange(256) 
    fig = figure(x_range=[0, c], y_range=[0, rows]) 
    fig.image_rgba(image=[img], x=[0], y=[0], dw=[cols], dh=[rows]) 
    return fig 

Più tardi voglio per ingrandire la figura:

fig = make_fig() 
# <- zoom in on plot, like `set_xlim` from matplotlib 
show(fig) 

Come posso fare zoom programmatico in bokeh?

risposta

20

Un modo è quello CAN le cose con un semplice tupla durante la creazione di una figura:

figure(..., x_range=(left, right), y_range=(bottom, top)) 

Ma è anche possibile impostare le x_range e y_range proprietà di una figura creata direttamente. (Ero stato alla ricerca di qualcosa di simile set_xlim o set_ylim da matplotlib.)

from bokeh.models import Range1d 

fig = make_fig() 
left, right, bottom, top = 3, 9, 4, 10 
fig.x_range=Range1d(left, right) 
fig.y_range=Range1d(bottom, top) 
show(fig) 
+0

Come implementarlo su y_axis_type = 'log'? Non funziona con quella parte inferiore e superiore. – asofyan

+5

Non funziona per me. Dovevo fare 'fig.x_range = Range1d (x_min, x_max)' –

+0

'set' era davvero un dettaglio di implementazione, e da allora è stato rimosso. Ho aggiornato la risposta. – bigreddot

0

è anche possibile utilizzarlo direttamente

p = Histogram(wind , xlabel= 'meters/sec', ylabel = 'Density',bins=12,x_range=Range1d(2, 16)) show(p)

+1

Non funziona per me. Ho dovuto fare 'p.x_range = Range1d (x_min, x_max)' –

2

Forse una soluzione ingenua, ma perché non passa per l'asse lim come argomento della tua funzione?

import numpy 
from bokeh.plotting import figure, show, output_notebook 
output_notebook() 

def make_fig(rows=16, cols=16,x_range=[0, 16], y_range=[0, 16], plot_width=500, plot_height=500): 
    img = numpy.ones((rows, cols), dtype=numpy.uint32) 
    view = img.view(dtype=numpy.uint8).reshape((rows, cols, 4)) 
    view[:, :, 0] = numpy.arange(256) 
    view[:, :, 1] = 265 - numpy.arange(256) 
    fig = figure(x_range=x_range, y_range=y_range, plot_width=plot_width, plot_height=plot_height) 
    fig.image_rgba(image=[img], x=[0], y=[0], dw=[cols], dh=[rows]) 
    return fig