2016-05-11 4 views
6

मैं समझता हूं कि बोके में दिखाने के लिए आप विशिष्ट टिक कैसे निर्दिष्ट करते हैं, लेकिन मेरा सवाल यह है कि यदि स्थिति बनाम दिखाने के लिए एक विशिष्ट लेबल असाइन करने का कोई तरीका है। इसलिए उदाहरण केबोके में टिकों के लिए मैं कस्टम लेबल का उपयोग कैसे करूं?

के लिए
plot.xaxis[0].ticker=FixedTicker(ticks=[0,1]) 

केवल 0 और 1 पर X- अक्ष लेबल, लेकिन क्या हुआ अगर बजाय 0 और 1 दिखाने का मैं एप्पल और ऑरेंज दिखाना चाहते थे दिखाएगा। कुछ

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

एक हिस्टोग्राम उस डेटा के लिए काम नहीं करेगा जो मैं प्लॉट कर रहा हूं। क्या बोके में कस्टम लेबल का उपयोग करने के लिए वैसे भी है?

उत्तर

3

EDIT: बोके 0.12.5 के लिए अपडेट किया गया है लेकिन दूसरे उत्तर में सरल विधि भी देखता है।

यह मेरे लिए काम किया:

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

' कौशल सूची और pct_counts बनाए गए थे, लेकिन यहां नहीं दिखाया गया है <--- --- किसी के लिए क्या अच्छा है ?????? – dopatraman

+0

हा, और यही कारण है कि आपने इसे वोट दिया? आप कोडेड संपादित कर सकते हैं और इसके बजाय रचनात्मक बना सकते हैं। – wordsforthewise

+0

फिक्स्ड, क्या आप अब अपने डाउनवोट को रिवर्स कर सकते हैं? – wordsforthewise

8

हाल Bokeh विज्ञप्ति के रूप में (जैसे 0.12.4 या नया), इस 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

पर बहुत अच्छा काम करता है उपयोगी लगता है, लेकिन यह सिर्फ मुझे अपने ब्राउज़र में एक खाली पृष्ठ देता है। – EddyTheB

+0

बोके '0.12.4' और '0.12.5' के साथ मेरे लिए काम करना इतना जांच करने के लिए कि क्यों हो सकता है। – bigreddot

+0

मैं 0.12.2 पर था, एक अपग्रेड ने इसे ठीक कर दिया है :-) – EddyTheB

संबंधित मुद्दे