2016-05-11 17 views
6

Capisco come si specificano le zecche specifiche da mostrare in Bokeh, ma la mia domanda è se c'è un modo per assegnare un'etichetta specifica da mostrare rispetto alla posizione. Così, per esempioCome utilizzare le etichette personalizzate per i segni di graduazione in Bokeh?

plot.xaxis[0].ticker=FixedTicker(ticks=[0,1]) 

mostrerà solo le etichette di asse x a 0 e 1, ma cosa succede se invece di mostrare 0 e 1 volevo mostrare Apple e Orange. Qualcosa come

plot.xaxis[0].ticker=FixedTicker(ticks=[0,1], labels=['Apple', 'Orange']) 

Un istogramma non funziona per i dati che sto tracciando. Esiste comunque l'opportunità di utilizzare etichette personalizzate in Bokeh come questa?

risposta

3

MODIFICA: aggiornato per Bokeh 0.12.5 ma vedere anche il metodo più semplice nell'altra risposta.

questo ha funzionato per me:

import pandas as pd 
from bokeh.charts import Bar, output_file, show 
from bokeh.models import TickFormatter 
from bokeh.core.properties import Dict, Int, String 

class FixedTickFormatter(TickFormatter): 
    """ 
    Class used to allow custom axis tick labels on a bokeh chart 
    Extends bokeh.model.formatters.TickFormatte 
    """ 

    JS_CODE = """ 
     import {Model} from "model" 
     import * as p from "core/properties" 

     export class FixedTickFormatter extends Model 
      type: 'FixedTickFormatter' 
      doFormat: (ticks) -> 
      labels = @get("labels") 
      return (labels[tick] ? "" for tick in ticks) 
      @define { 
      labels: [ p.Any ] 
      } 
    """ 

    labels = Dict(Int, String, help=""" 
    A mapping of integer ticks values to their labels. 
    """) 

    __implementation__ = JS_CODE 

skills_list = ['cheese making', 'squanching', 'leaving harsh criticisms'] 
pct_counts = [25, 40, 1] 
df = pd.DataFrame({'skill':skills_list, 'pct jobs with skill':pct_counts}) 
p = Bar(df, 'index', values='pct jobs with skill', title="Top skills for ___ jobs", legend=False) 
label_dict = {} 
for i, s in enumerate(skills_list): 
    label_dict[i] = s 

p.xaxis[0].formatter = FixedTickFormatter(labels=label_dict) 
output_file("bar.html") 
show(p) 

result of code

+0

' lista delle abilità e pct_count sono stati creati, ma non mostrati qui! <--- di che cosa è buono per chiunque ?????? – dopatraman

+0

Ha, ed è per questo che hai votato? Potresti aver modificato il codice ed essere stato invece costruttivo. – wordsforthewise

+0

Risolto, puoi invertire il tuo downvote ora per favore? – wordsforthewise

8

Dal recenti uscite Bokeh (ad es 0.12.4 o più recente), questo è ora molto più semplice da realizzare utilizzando FuncTickFormatter:

import pandas as pd 
from bokeh.charts import Bar, output_file, show 
from bokeh.models import FuncTickFormatter 

skills_list = ['cheese making', 'squanching', 'leaving harsh criticisms'] 
pct_counts = [25, 40, 1] 
df = pd.DataFrame({'skill':skills_list, 'pct jobs with skill':pct_counts}) 
p = Bar(df, 'index', values='pct jobs with skill', title="Top skills for ___ jobs", legend=False) 
label_dict = {} 
for i, s in enumerate(skills_list): 
    label_dict[i] = s 

p.xaxis.formatter = FuncTickFormatter(code=""" 
    var labels = %s; 
    return labels[tick]; 
""" % label_dict) 

output_file("bar.html") 
show(p) 
+0

Sembra utile, ma mi dà solo una pagina vuota nel mio browser. – EddyTheB

+0

Lavorando per me con Bokeh '0.12.4' e' 0.12.5' quindi sono necessarie ulteriori informazioni per indagare sul perché potrebbe essere. – bigreddot

+0

Ero su 0.12.2, un aggiornamento lo ha risolto :-) – EddyTheB

Problemi correlati