简体   繁体   English

散景图像的较小范围填充

[英]Smaller range padding for image plot with bokeh

I'm using bokeh 1.0.4 and I would like to generate an image plot in bokeh using match_aspect=True . 我正在使用bokeh 1.0.4,我想使用match_aspect=True在bokeh中生成图像图。 Here is a example code for illustration: 这是用于说明的示例代码:

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: 这是我得到的: 没有给定范围的图

There is a lot empty space around the data, too much for my application, and I would like to have the axes more tight - knowing that this currently cannot perfectly be done, see this other question in section "Update". 数据周围有很多空白,对我的应用程序来说太大了,我想使轴更紧-知道当前无法完美完成,请参见“更新”部分中的另一个问题

So I've tried to use the parameter range_padding , which should be relative to the image dimensions (default unit: percent), but it doesn't work for me, eg if I use 所以我尝试使用参数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)

the padding is even larger: 填充更大:

使用<code> range_padding </ code>进行绘图

Small padding values like 0.05 seem to have no effect. 较小的填充值(如0.05似乎无效。 Also I cannot use start and end arguments for the ranges because then the matching aspect ratio is lost. 另外,我不能使用范围的startend参数,因为这样会丢失匹配的宽高比。 A square in data space should match a square on the screen. 数据空间中的正方形应与屏幕上的正方形匹配。

Did I miss something in the way I use the range_padding parameters here? 我是否在这里使用range_padding参数时错过了某些东西? Does anybody have an idea how to reduce the space around the image such that the matching aspect is kept? 有谁知道如何减少图像周围的空间以保持匹配的外观?

Update 更新

I would like not to set plot's height and width to fixed values, because I also want to add a colorbar and maybe other things later and this will increase the plot dimensions in an unpredictable way such that the aspect ratios don't match any more. 我不想将绘图的高度和宽度设置为固定值,因为我还想稍后添加颜色条,也许还要添加其他内容,这将以一种无法预测的方式增加绘图尺寸,以致长宽比不再匹配。

Is this work-around acceptable for you (Bokeh v1.0.4)? 您可以接受这种解决方法(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: 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM