对齐散景图. (Python) [英] Aligning a Bokeh plot. (Python)

查看:59
本文介绍了对齐散景图. (Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是Python的Bokeh库的初学者.

I am actually a beginner to Python's Bokeh library.

我正在绘制一个简单的散点图,如下所示:

I am plotting a simple scatter plot as follows:

from bokeh.io import show
from bokeh.plotting import figure
plot = figure()
plot.circle(x=[1,2,3,4,5],y=[10,7,2,5,9],size=10)
show(plot)

上面的代码打开一个html页面,该图与屏幕左侧对齐.

The code above opens a html page, with that plot aligned to the left of the screen.

有没有一种方法可以使绘图与中心对齐?

Is there a way to align the plot to the center ?

我实际上看到了方法的服装 align .

I actually saw an attibute align of the method figure.

我将其设置为居中",但什么也没发生.

I set it to 'center', but nothing happened.

任何人都可以帮忙吗?

推荐答案

好吧,我遇到了这个问题,并且找到了一个可行的解决方案: 您需要创建一个模板,该模板将覆盖图的父div的margin属性.

Well, I have faced this problem and i found a solution that works: You need to create a template that will overwrite the margin property of the plot's parent div>

template = """
{% block postamble %}
<style>
.bk-root .bk {
    margin: 0 auto !important;
}
</style>
{% endblock %}
"""

.bk-root .bk 定义绘图的父div.也指定.bk-root,因为这将防止通过特定样式(与样式重要性评估顺序有关)忽略此属性. !important; 是必需的,因为该div的margin属性是内联定义的.内联属性只能使用此!important标记覆盖.

.bk-root .bk defines the parent div of your plot. .bk-root is specified too because this will prevent ignoring of this property by specific styles(related to style importance evaluation order). !important; is needed because margin property for that div is defined inline. Inline property can be overwritten only with this !important tag.

此后,您将需要两次导入和两次函数调用:

After this you will need two imports and two function calls:

from bokeh.io import save
from bokeh.util.browser import view
save(your_plot, template=template)
view("digits.html")

保存功能会将模板应用于您的html文档.查看功能将在浏览器中打开一个带有居中绘图的新选项卡.

The save function will apply template to your html document. The view function will open a new tab in browser with your centered plot.

如果此解决方案不起作用,则应从浏览器检查html代码,并找出应为哪个div类修改margin属性,但是一般概念将保持不变.

In case this solution does not work, you should inspect the html code from your browser and find out for which div class you should modify margin property, but general concept will remain the same.

这篇关于对齐散景图. (Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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