与等轴的散景图

时间:2014-10-31 12:31:49

标签: python axes bokeh

等轴的散景图

我使用PythonBokeh创建了一个Plot(参见代码)。

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

2 个答案:

答案 0 :(得分:12)

截至Bokeh 0.12.7,此功能已实施。 Plots现在可以接受两个新属性。 p = figure(match_aspect=True) p.circle([-1, +1, +1, -1], [-1, -1, +1, +1]) ,当设置为true时,将匹配数据空间的方面与绘图的像素空间。例如,以数据单位绘制的正方形现在也将是以像素为单位的完美正方形。

aspect_scale

Bokeh match aspect = True

match_aspect允许您通过在p = figure(aspect_scale=2) p.circle([-1, +1, +1, -1], [-1, -1, +1, +1]) 进行的宽高比修正之上指定乘数来进一步控制宽高比。

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

Bokeh aspect scale = 2

$empty = $form_state = array();
$form_state['uid'] = $uid; // your user id
user_login_submit($empty, $form_state);

Bokeh aspect scale = 0.5

答案 1 :(得分:3)

可悲的是,似乎两年后这个功能仍然缺失。作为一种解决方法,我编写了一个函数,可以适当地设置图形的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)