散景图像绘图的较小范围填充

2024-04-02

我正在使用 bokeh 1.0.4,我想使用 bokeh 生成图像图match_aspect=True。这是用于说明的示例代码:

from bokeh.models.ranges import DataRange1d
from bokeh.plotting import figure, show
import numpy as np

arr = np.array([[1,2,3],[4,5,6]])

plot = figure(match_aspect=True)
plot.image([arr], x=0, y=0, dw=3, dh=2)

show(plot)

This is what I get: Plot without given ranges

数据周围有很多空白空间,对于我的应用程序来说太多了,我希望轴更紧 - 知道目前无法完美完成此操作,请参阅此其他问题 https://stackoverflow.com/questions/53617819/image-plot-in-bokeh-with-tight-axes-and-matching-aspect-ratio在“更新”部分。

所以我尝试使用该参数range_padding,它应该与图像尺寸相关(默认单位:百分比),但它对我不起作用,例如如果我使用

from bokeh.models.ranges import DataRange1d
from bokeh.plotting import figure, show
import numpy as np

arr = np.array([[1,2,3],[4,5,6]])

x_range = DataRange1d(range_padding=5, range_padding_units='percent')
y_range = DataRange1d(range_padding=5, range_padding_units='percent')

plot = figure(x_range=x_range, y_range=y_range, match_aspect=True)
plot.image([arr], x=0, y=0, dw=3, dh=2)

show(plot)

填充甚至更大:

小填充值,例如0.05似乎没有效果。我也不能使用start and end范围的参数,因为那么 匹配的宽高比丢失。数据空间中的正方形应与屏幕上的正方形匹配。

我在使用的方式中错过了什么吗?range_padding参数在这里? 有谁知道如何减少图像周围的空间,这样 匹配方面是否保留?

Update

我不想将绘图的高度和宽度设置为固定值,因为我还想添加一个颜色条,也许稍后添加其他东西,这将以不可预测的方式增加绘图尺寸,使得纵横比不再匹配。


您可以接受此解决方法吗(Bokeh v1.0.4)?

from bokeh.models.ranges import DataRange1d
from bokeh.plotting import figure, show
from bokeh.layouts import Row
from bokeh.palettes import Greys
from bokeh.models import LinearColorMapper, ColorBar
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

sx = 3
sy = 2

x_range = DataRange1d(start = 0, end = sx, bounds = (0, sx), range_padding = 5, range_padding_units = 'percent')
y_range = DataRange1d(start = 0, end = sy, bounds = (0, sy), range_padding = 5, range_padding_units = 'percent')

pw = 400
ph = pw * sy / sx
plot = figure(plot_width = pw,
              plot_height = ph,
              x_range = x_range,
              y_range = y_range,
              match_aspect = True)
plot.image([arr], x = 0, y = 0, dw = sx, dh = sy)

color_mapper = LinearColorMapper(palette = Greys[6], low = arr.min(), high = arr.max())
colorbar_plot = figure(plot_height = ph, plot_width = 69, x_axis_location = None, y_axis_location = None, title = None, tools = '', toolbar_location = None)
colorbar = ColorBar(color_mapper = color_mapper, location = (0, 0))
colorbar_plot.add_layout(colorbar, 'left')

show(Row(plot, colorbar_plot))

Result:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

散景图像绘图的较小范围填充 的相关文章

随机推荐