2014-10-31 10 views
7

बराबर कुल्हाड़ियों

साथ Bokeh प्लॉट मैं Python पुस्तकालय Bokeh (कोड देखें) के साथ एक प्लॉट बनाया।बराबर कुल्हाड़ियों साथ Bokeh प्लॉट

from bokeh.plotting import * 

figure() 
hold() 
rect([1,3], [1,1], [1,0.5], [1,0.5]) 
patch([0,0,4,4], [2,0,0,2], line_color="black", fill_color=None) 
show() 

मैं कैसे आदेश axis('equal') साथ matplotlib में के रूप में वर्गों बराबर कुल्हाड़ियों के साथ (एक ही चौड़ाई और ऊंचाई के साथ आयत) का प्रतिनिधित्व कर सकते हैं?

http://matplotlib.org/examples/pylab_examples/axis_equal_demo.html

मैं चौड़ाई और साजिश की ऊंचाई बदलने के लिए या इस समस्या को हल करने के लिए अक्ष रेंज लेकिन मुझे लगता है, वहाँ एक चालाक विकल्प होना चाहिए निर्धारित करने का विकल्प देखें।

नोट: मैं Python v.2.7.8 और Bokeh v.0.6.1 का उपयोग कर रहा हूं।

+0

यह एक खुला मुद्दा है: https://github.com/bokeh/bokeh/issues/474 – bigreddot

+0

लिंक के लिए धन्यवाद। मैं एक समाधान का उपयोग करने के लिए तत्पर हूं। – Spirou

+1

कृपया 0.12.7 – bigreddot

उत्तर

2

अफसोस की बात है कि ऐसा लगता है कि दो साल बाद यह सुविधा अभी भी गायब है। एक कामकाज के रूप में, मैंने एक ऐसा फ़ंक्शन लिखा है जो x_range और y_range किसी दिए गए पहलू अनुपात के साथ अपने डेटा को प्रदर्शित करने के लिए उचित रूप से एक आकृति के गुण सेट करता है। यह तब तक ठीक काम करता है जब तक आप किसी भी उपकरण को बॉक्स ज़ूम की अनुमति नहीं देते हैं जिससे उपयोगकर्ता पहलू अनुपात को संशोधित कर दे।

__all__ = ['set_aspect'] 

from bokeh.models import Range1d 

def set_aspect(fig, x, y, aspect=1, margin=0.1): 
    """Set the plot ranges to achieve a given aspect ratio. 

    Args: 
     fig (bokeh Figure): The figure object to modify. 
     x (iterable): The x-coordinates of the displayed data. 
     y (iterable): The y-coordinates of the displayed data. 
     aspect (float, optional): The desired aspect ratio. Defaults to 1. 
     Values larger than 1 mean the plot is squeezed horizontally. 
     margin (float, optional): The margin to add for glyphs (as a fraction 
     of the total plot range). Defaults to 0.1 
    """ 
    xmin = min(xi for xi in x) 
    xmax = max(xi for xi in x) 
    ymin = min(yi for yi in y) 
    ymax = max(yi for yi in y) 
    width = (xmax - xmin)*(1+2*margin) 
    if width <= 0: 
     width = 1.0 
    height = (ymax - ymin)*(1+2*margin) 
    if height <= 0: 
     height = 1.0 
    xcenter = 0.5*(xmax + xmin) 
    ycenter = 0.5*(ymax + ymin) 
    r = aspect*(fig.plot_width/fig.plot_height) 
    if width < r*height: 
     width = r*height 
    else: 
     height = width/r 
    fig.x_range = Range1d(xcenter-0.5*width, xcenter+0.5*width) 
    fig.y_range = Range1d(ycenter-0.5*height, ycenter+0.5*height) 

if __name__ == '__main__': 
    from bokeh.plotting import figure, output_file, show 

    x = [-1, +1, +1, -1] 
    y = [-1, -1, +1, +1] 
    output_file("bokeh_aspect.html") 
    p = figure(plot_width=400, plot_height=300, tools='pan,wheel_zoom', 
       title="Aspect Demo") 
    set_aspect(p, x, y, aspect=2) 
    p.circle(x, y, size=10) 
    show(p) 
+0

में 'match_aspect' के संबंध में नीचे @DuCorey द्वारा नया उत्तर दें, FYI बॉक्स ज़ूम अब (0.12' dev builds के रूप में) साजिश के मौजूदा पहलू का सम्मान करने के लिए कॉन्फ़िगर किया जा सकता है। – bigreddot

+0

समय सीमा के संबंध में, स्थिर छवियों को उत्पन्न करने वाली लाइब्रेरी के लिए, पहलू नियंत्रण एक छोटी सी सुविधा होगी। दुर्भाग्यवश, बोके ने बड़े ब्राउज़र लेआउट संदर्भ में भूखंडों को एम्बेड किया है, और "वेब प्रतिक्रिया" (एक निरंतर अनुरोधित सुविधा) के साथ निश्चित पहलू को सुलझाने की कोशिश कर रहा है, यह एक बहुत कांटेदार समस्या बन जाता है। हम इस पर काम कर रहे हैं, हमारे सीमित संसाधन परमिट के रूप में सबसे अच्छे और तेज़। – bigreddot

+0

यह समाधान बहुत अच्छा है। हालांकि 0.12.5 सर्वर पर चलाने के लिए इसे कार्यान्वित करते समय, मुझे इसके लिए काम शुरू करने के लिए सीधे श्रेणी की शुरुआत और अंत गुणों को संशोधित करना पड़ा। 'fig.x_range.start = xcenter - 0.5 * चौड़ाई fig.x_range.end = xcenter + 0.5 * चौड़ाई fig.y_range.start = ycenter - 0.5 * ऊंचाई fig.y_range.end = ycenter + 0.5 * height' – DuCorey

5

बोके 0.12.7 के रूप में, यह सुविधा लागू की गई है। भूखंड अब दो नई संपत्तियों को स्वीकार कर सकते हैं। match_aspect जो, जब सत्य पर सेट किया जाता है, तो डेटा स्पेस के पहलू को साजिश के पिक्सेल स्पेस से मेल करेगा। उदाहरण के लिए, डेटा इकाइयों में खींचे गए वर्ग अब पिक्सेल इकाइयों में भी सही वर्ग होंगे।

p = figure(match_aspect=True) 
p.circle([-1, +1, +1, -1], [-1, -1, +1, +1]) 

Bokeh match aspect = True

aspect_scale आप आगे पहलू match_aspect द्वारा किए गए सुधार के शीर्ष पर एक गुणक निर्दिष्ट द्वारा पहलू अनुपात को नियंत्रित करने के लिए अनुमति देता है।

p = figure(aspect_scale=2) 
p.circle([-1, +1, +1, -1], [-1, -1, +1, +1]) 

Bokeh aspect scale = 2

p = figure(aspect_scale=0.5) 
p.circle([-1, +1, +1, -1], [-1, -1, +1, +1]) 

Bokeh aspect scale = 0.5

+0

यदि आप इन आंकड़ों को उत्पन्न करने के लिए उपयोग किए गए कोड को जोड़ते हैं तो यह निर्देशक होगा। – jorgeh

+1

@jorgeh टिप्पणी के लिए धन्यवाद। मैंने कोड जोड़ा है जिसका उपयोग आंकड़े उत्पन्न करने के लिए किया गया था। – DuCorey

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