散景图像的较小范围填充 [英] Smaller range padding for image plot with bokeh

查看:95
本文介绍了散景图像的较小范围填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using bokeh 1.0.4 and I would like to generate an image plot in bokeh using match_aspect=True. 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)

这是我得到的:

数据周围有很多空白,对我的应用程序来说太大了,我想使轴更紧密-知道当前无法完美完成,请参见此.

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".

因此,我尝试使用参数range_padding,该参数应相对于图像尺寸(默认单位:百分比),但不适用于我,例如如果我使用

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, e.g. if I use

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这样的小填充值似乎无效.另外,我不能为范围使用startend参数,因为这样 匹配的宽高比丢失.数据空间中的正方形应与屏幕上的正方形匹配.

Small padding values like 0.05 seem to have no effect. Also I cannot use start and end arguments for the ranges because then the matching aspect ratio is lost. A square in data space should match a square on the screen.

我在这里使用range_padding参数的方式错过了吗? 是否有人知道如何缩小图像周围的空间,以至于 匹配方面是否保留?

Did I miss something in the way I use the range_padding parameters here? Does anybody have an idea how to reduce the space around the image such that the matching aspect is kept?

我不想将绘图的高度和宽度设置为固定值,因为我还想稍后添加颜色条和其他可能的东西,这将以一种无法预测的方式增加绘图尺寸,从而使宽高比不匹配还有.

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.

推荐答案

您是否可以接受此变通办法(Bokeh v1.0.4)?

Is this work-around acceptable for you (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))

结果:

这篇关于散景图像的较小范围填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆