使用本地文件中的背景图像绘制布局 [英] Plotly layout with background image from local file

查看:47
本文介绍了使用本地文件中的背景图像绘制布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the same problem as stated in this question: I want my plot to have a background from a local png file.

使用 plotly 的示例(使用来自网络的图像)有效.

Using plotly's example, which uses an image from the web, works.

使用我的本地图像的路径,无论是否使用绝对路径,都不起作用.

Using a path to my local image, with or without absolute path, does not work.

根据另一个问题的答案进行编码无济于事.

Encoding as per the answer to this other question does not help.

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)

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

How hard can it be to get a local image as background? How do you do it?

推荐答案

我需要阅读 imagesxy 参数> 字典.在测试编码解决方案之前,我已经更改了 y,图像出现在情节之外.多么愚蠢.将 y 设置为 3 可以修复它.

I needed to read about the x and y parameters of the images dictionary. I had changed the y before testing the encoding solution, and the image appeared way outside the plot. How silly. Setting y to 3 instead fixes it.

这篇关于使用本地文件中的背景图像绘制布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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