Plotly 4.8,应用时模板不添加图像布局
Plotly 4.8, template not adding image layout when applied
我正在构建一个 plotly 模板,以减少“美化”R&D 可视化以用于演示文稿的工作。
在尝试通过模板将徽标添加到绘图时,我遇到了一些烦人的事情。
向模板添加徽标图像非常简单。当稍后应用模板时,在这种情况下通过 pandas plot with the plotly backend,不会添加图像。图像布局存在于图形模板中,可以检索并分配给图形的 layout.images,显示图像。
代码段要求 plotly 4.8 或更高。
import plotly.graph_objs as go
import plotly.io as pio
import pandas as pd
import plotly.express as px
pd.options.plotting.backend = "plotly"
# Add a test image to the template layout
pio.templates['test_template'] = go.layout.Template({'layout': {
'images': [{'sizex': 0.5, 'sizey': 0.5,
'source': ('https://upload.wikimedia.org/wikipedia/commons/8/85/Logo-Test.png'),
'x': 0.5, 'xanchor': 'center', 'xref': 'paper',
'y': 0, 'yanchor': 'bottom', 'yref': 'paper'}],
}})
# apply template when plotting mock data, does not display test logo image.
fig = pd.DataFrame(dict(a=[1, 2, 3], b = [3, 2, 1])).plot.bar(template = "test_template")
fig.show()
# Retrieving the image layout from within the figure template makes the image appear
fig.layout.images=fig.layout.template.layout.images
fig.show()
我在将 fig.update_layout
与模板一起使用时发现了相同的行为。通过 go.Figure(layout=template.layout)
应用模板的布局部分时显示图像。
我在这里遗漏了什么吗?
啊,抱歉这里的文档很神秘,但是您需要在布局图像中设置另一个属性名称,以使其实际出现在使用模板的图形中:https://plotly.com/python/reference/layout/images/#layout-images-items-image-name
这样设置是为了让使用模板的图表可以“有条件地使用”模板属性,即您设置模板的图像部分,使徽标始终位于同一位置,但徽标仅在以下情况下出现URL 设置在一个图形中,允许一个模板用于带有各种徽标的图形,例如。
我正在构建一个 plotly 模板,以减少“美化”R&D 可视化以用于演示文稿的工作。
在尝试通过模板将徽标添加到绘图时,我遇到了一些烦人的事情。
向模板添加徽标图像非常简单。当稍后应用模板时,在这种情况下通过 pandas plot with the plotly backend,不会添加图像。图像布局存在于图形模板中,可以检索并分配给图形的 layout.images,显示图像。
代码段要求 plotly 4.8 或更高。
import plotly.graph_objs as go
import plotly.io as pio
import pandas as pd
import plotly.express as px
pd.options.plotting.backend = "plotly"
# Add a test image to the template layout
pio.templates['test_template'] = go.layout.Template({'layout': {
'images': [{'sizex': 0.5, 'sizey': 0.5,
'source': ('https://upload.wikimedia.org/wikipedia/commons/8/85/Logo-Test.png'),
'x': 0.5, 'xanchor': 'center', 'xref': 'paper',
'y': 0, 'yanchor': 'bottom', 'yref': 'paper'}],
}})
# apply template when plotting mock data, does not display test logo image.
fig = pd.DataFrame(dict(a=[1, 2, 3], b = [3, 2, 1])).plot.bar(template = "test_template")
fig.show()
# Retrieving the image layout from within the figure template makes the image appear
fig.layout.images=fig.layout.template.layout.images
fig.show()
我在将 fig.update_layout
与模板一起使用时发现了相同的行为。通过 go.Figure(layout=template.layout)
应用模板的布局部分时显示图像。
我在这里遗漏了什么吗?
啊,抱歉这里的文档很神秘,但是您需要在布局图像中设置另一个属性名称,以使其实际出现在使用模板的图形中:https://plotly.com/python/reference/layout/images/#layout-images-items-image-name
这样设置是为了让使用模板的图表可以“有条件地使用”模板属性,即您设置模板的图像部分,使徽标始终位于同一位置,但徽标仅在以下情况下出现URL 设置在一个图形中,允许一个模板用于带有各种徽标的图形,例如。