【问题标题】:Plotly layout with background image from local file使用本地文件中的背景图像进行布局
【发布时间】:2019-04-25 04:59:38
【问题描述】:

我遇到了与this question 中所述相同的问题:我希望我的情节具有来自本地 png 文件的背景。

使用plotly's example,它使用来自网络的图像,有效。

使用我的本地图像的路径,无论有没有绝对路径,都不起作用。

按照其他问题的答案进行编码没有帮助。

import base64
import os
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

import numpy as np

trace1= go.Scatter(x=[0,0.5,1,2,2.2],y=[1.23,2.5,0.42,3,1])

with open(os.getcwd() + "/resources/office_map.png", "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read()).decode()
# Add the prefix that plotly will want when using the string as source
encoded_image = "data:image/png;base64," + encoded_string

layout= go.Layout(
    title='My title',
    images= [dict(
        source= encoded_image,
        xref= "x",
        yref= "y",
        x= 0,
        y= 10,
        sizex= 744,
        sizey= 1052,
        sizing= "stretch",
        opacity= 0.5,
        layer= "below")])

fig=go.Figure(data=[trace1],layout=layout)
plot(fig)

获取本地图像作为背景有多难?你是怎么做到的?

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    我需要阅读 images 字典的 xy 参数。我在测试编码解决方案之前更改了y,图像出现在情节之外。多么愚蠢。将 y 设置为 3 可以解决此问题。

    【讨论】:

      猜你喜欢
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 2019-11-14
      相关资源
      最近更新 更多