散景圈不适合正方形吗? [英] Bokeh Circle does not fit into square?

查看:65
本文介绍了散景圈不适合正方形吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用bokeh绘制一些几何图形,并遇到了这个问题.我正在绘制一个具有相等边(即正方形)的矩形,然后在那个正方形中绘制一个直径=正方形宽度的圆形.该圆应在边缘处与正方形相切,但并非如此.

I am plotting some geometry using bokeh and came across this. I am plotting a rectangle with equal sides (i.e. a square), and in that square, plotting a circle with diameter = width of the square. The circle should tangent to the square at edges, but it is not.

下面是代码:

from bokeh.plotting import output_notebook, figure, show
output_notebook()

p = figure(width=500, height=500)

p.rect(0, 0, 300, 300, line_color='black')
p.circle(x=0, y=0, radius=150, line_color='black', 
         fill_color='grey',  radius_units='data')

p.axis.minor_tick_out = 0

show(p)

这将导致以下结果:

我在做任何错误的事情还是可以改变以使圆正好适合正方形?

Is there anything I am doing wrong or could change to make the circle fit exactly in the square?

预先感谢,兰德尔

这是另一种情况-仅画一个圆圈:

Here's another case - just drawing a circle:

p = figure(width=500, height=500, x_range=(-150, 150), y_range=(-150, 150))
p.circle(x=0, y=0, radius=150, line_color='black', 
         fill_color='grey', radius_units='data')
show(p)

圆的半径在x方向上为150,但在y方向上不是.

radius of the circle is 150 in the x direction, but not the y-direction.

推荐答案

我想报告的是,从Bokeh 0.12.7开始,现在可以以更简单的方式解决此问题.

I would like to report that as of Bokeh 0.12.7, this issue can now be fixed in a simpler manner.

如其他文章所述,主要问题不是圆形不是圆形,而是正方形不是正方形.这是由于这样的事实,即在默认情况下,甚至在宽度和高度设置为相同值的情况下,Bokeh在其上绘制图形(画布)的实际区域通常也不是正方形.散景默认情况下会尝试用完画布上的所有空间来绘制图形.这会在图的 data 距离和 pixel 距离之间造成不匹配.

As described in other posts, the main issue is not that the circle is not a circle, but that the square is not a square. This is due to the fact that actual area on which Bokeh draws the figure (the canvas) is usually not a square by default or even when the width and height are set to the same value. Bokeh by default will attempt to draw a figure by using up all the space on the canvas. This creates a mismatch between the data distance and the pixel distance of the plot.

从0.12.7开始,图形现在可以接受 match_aspect 属性,该属性设置为True时,会将 data 空间的长宽比匹配到像素的空间.

As of 0.12.7, figures can now accept a match_aspect property which when set to True will will match the aspect of the data space to the pixel space of the plot.

在您的示例中,只需在图中添加 match_aspect = True

In your example, simply adding the match_aspect = True in your figure

p = figure(width=500, height=500, match_aspect=True, 
           title="Circle touches all 4 sides of square")
p.rect(0, 0, 300, 300, line_color='black')
p.circle(x=0, y=0, radius=150, line_color='black', 
         fill_color='grey',  radius_units='data')

现在将产生

这篇关于散景圈不适合正方形吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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