2014-10-31 63 views
7

散景劇情具有相等軸

我創建與PythonBokeh(見代碼)的曲線圖。散景劇情具有相等軸

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.8Bokeh v.0.6.1

+0

這是一個懸而未決的問題:https://github.com/bokeh/bokeh/issues/474 – bigreddot 2014-11-04 17:11:06

+0

感謝您的鏈接。我期待着使用一個解決方案。 – Spirou 2014-11-07 12:13:53

+1

請注意@DuCorey關於'match_aspect'的0.12.7 – bigreddot 2017-08-30 14:52:56

回答

2

不幸的是,似乎兩年後這個功能仍然缺失。作爲一種解決方法,我已經編寫了一個函數,可以適當地設置圖的x_rangey_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

的新答案現在可以將FYI框縮放(從'0.12'開發版本開始)配置爲尊重劇情的現有方面。 – bigreddot 2016-04-26 19:04:31

+0

關於時間框架,對於生成靜態圖像的庫,方面控制將是一個微不足道的功能。不幸的是,Bokeh在更大的瀏覽器佈局環境中嵌入了圖表,並試圖將固定方面與「網絡響應」(也是一個連續請求的功能)進行協調,這成爲一個非常棘手的問題。我們正在盡力而爲,盡我們有限的資源許可。 – bigreddot 2016-04-26 19:07:37

+0

這個解決方案非常棒。然而,當它在0.12.5服務器上運行時,我不得不直接修改範圍的開始和結束屬性以使其工作。 'fig.x_range.start = xcenter - 0.5 * width fig.x_range.end = xcenter + 0.5 * width fig.y_range.start = ycenter - 0.5 * height fig.y_range.end = ycenter + 0.5 * height' – DuCorey 2017-07-06 21:49:54

5

從散景0.12.7開始,此功能已實施。情節現在可以接受兩個新的屬性。 match_aspect當設置爲true時,它將將數據空間的方面與圖的像素空間相匹配。例如,以數據單位繪製的正方形現在也是像素單位的完美正方形。

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 2017-10-17 06:24:14

+1

@jorgeh感謝您的評論。我添加了用於生成數字的代碼。 – DuCorey 2017-10-19 14:21:45