2016-10-03 7 views
7

मैं डेटा लोड करने और/या संशोधित करने के लिए लूप का उपयोग करना चाहता हूं और परिणाम बोके के उपयोग से लूप के भीतर साजिश करना चाहता हूं (मैं Matplotlib's axes.color_cycle से परिचित हूं)। यहाँ एक सरल उदाहरणबोके के साथ साजिश करते समय, आप स्वचालित रूप से रंग पैलेट के माध्यम से कैसे चक्र करते हैं?

import numpy as np 
from bokeh.plotting import figure, output_file, show 
output_file('bokeh_cycle_colors.html') 

p = figure(width=400, height=400) 
x = np.linspace(0, 10) 

for m in xrange(10): 
    y = m * x 
    p.line(x, y, legend='m = {}'.format(m)) 

p.legend.location='top_left' 
show(p) 

जो इस साजिश

bokeh plot

उत्पन्न करता है मैं यह इतना रंग की एक सूची है और एक मापांक आपरेशन अप कोडिंग के बिना रंग चक्र को दोहराने के लिए कैसे कर सकता हूँ जब संख्या रंग खत्म हो जाता है?

इससे संबंधित गिटहब पर कुछ चर्चा हुई, 351 और 2201 समस्याएं, लेकिन यह स्पष्ट नहीं है कि यह कार्य कैसे करें। के लिए documentation पर खोज करते समय मुझे मिली चार हिट्स में वास्तव में पृष्ठ पर कहीं भी cycle शब्द नहीं था।

+0

[रंग साइक्लर] (http : //matplotlib.org/cycler/) पैकेज, matplotlib के हिस्से के रूप में विकसित, एक शब्दकोश बनाने के लिए उपयोगी हो सकता है जिसका उपयोग केवल रंग से अधिक चक्र के लिए किया जा सकता है। –

उत्तर

9

यह शायद सबसे आसान सिर्फ रंग की सूची और चक्र यह अपने आप itertools का उपयोग कर पाने के लिए है:

import numpy as np 
from bokeh.plotting import figure, output_file, show 

# select a palette 
from bokeh.palettes import Dark2_5 as palette 
# itertools handles the cycling 
import itertools 

output_file('bokeh_cycle_colors.html') 

p = figure(width=400, height=400) 
x = np.linspace(0, 10) 

# create a color iterator 
colors = itertools.cycle(palette)  

for m, color in itertools.izip(xrange(10), colors): 
    y = m * x 
    p.line(x, y, legend='m = {}'.format(m), color=color) 

p.legend.location='top_left' 
show(p) 

enter image description here

वहाँ bokeh/charts/utils.py में एक cycle_colors समारोह होने लगते है, लेकिन यह नहीं है प्रतीत होता है कि लाइब्रेरी में कहीं और कहा जाता है, और ऐसा लगता है कि यह वही काम कर रहा है।

import numpy as np 
from bokeh.plotting import figure, output_file, show 
# seaborn handles the color palette generation, itertools handles the cycling 
import seaborn.apionly as sns 
import itertools 

output_file('bokeh_cycle_colors.html') 

p = figure(width=400, height=400) 
x = np.linspace(0, 10) 

# define the color palette 
ncolors = 5 
palette = sns.palettes.color_palette('colorblind', ncolors) 
# as hex is necessary for bokeh to render the colors properly. 
colors = itertools.cycle(palette.as_hex()) 

for m, color in itertools.izip(xrange(10), colors): 
    y = m * x 
    p.line(x, y, legend='m = {}'.format(m), color=color) 

p.legend.location='top_left' 
show(p) 

enter image description here

5

दो छोटे परिवर्तन से पहले जवाब काम कर देगा:

यहाँ एक उदाहरण कई seaborn में उपलब्ध पट्टियाँ में से एक का प्रदर्शन (वे कुछ भी आप और अधिक matplotlib से उम्मीद होती है) है अजगर 3. के लिए

  • बदल दिया है: for m, color in zip(range(10), colors):

  • पूर्व: for m, color in itertools.izip(xrange(10), colors):

+0

मेरे लिए यह एक अलग उत्तर के बजाय एक संपादन के रूप में अधिक समझदार होगा (हालांकि मुझे लगता है कि आपकी वर्तमान प्रतिष्ठा के साथ आप संपादित नहीं कर सकते हैं)। –

1

सिर्फ तुम्हारे लिए एक सरल जनरेटर कि चक्र रंग परिभाषित करते हैं।

अजगर 2 में:

from bokeh.palettes import Category10 
import itertools 

def color_gen(): 
    for c in itertools.cycle(Category10[10]): 
     yield c 
color = color_gen() 

या अजगर 3 में:

from bokeh.palettes import Category10 
import itertools 

def color_gen(): 
    yield from itertools.cycle(Category10[10]) 
color = color_gen() 

और जब आप एक नया रंग की जरूरत है, कार्य करें:

p.line(x, y1, line_width=2, color=next(color)) 
p.line(x, y2, line_width=2, color=next(color)) 
संबंधित मुद्दे

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