def set_aspect(fig, x, y, aspect=1, margin=0.1):
"""Set the plot ranges to achieve a given aspect ratio.
https://stackoverflow.com/questions/26674779/bokeh-plot-with-equal-axes
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, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)
width = (xmax - xmin) * (1 + 2 * margin)
width = 1.0 if width <= 0 else width
height = (ymax - ymin) * (1 + 2 * margin)
height = 1.0 if height <= 0 else height
r = aspect * (fig.plot_width / fig.plot_height)
if width < r * height:
width = r * height
else:
height = width / r
xcenter = 0.5 * (xmax + xmin)
ycenter = 0.5 * (ymax + ymin)
fig.x_range = Range1d(xcenter - 0.5 * width, xcenter + 0.5 * width)
fig.y_range = Range1d(ycenter - 0.5 * height, ycenter + 0.5 * height)
评论列表
文章目录