阴谋的背景图像 [英] Plotly background images

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

问题描述

我正在尝试使用plotly在图形上获取背景图像.如果不使用images.plot.ly链接之一,我似乎无法显示任何图像.我在与项目相同的文件夹中尝试了Web URL和本地图像.这是我使用他们教程中的图像URL时显示的内容:

I'm trying to get a background image on my graph using plotly. I can't seem to get ANY image to display without using one of the images.plot.ly links. I tried web URLs and local images in the same folder as the project. This is what is showing up when I used the image URL from their tutorial:

https://plot.ly/python/images/

这是我唯一一次可以显示任何内容的时间.任何其他链接都不会在图形上产生任何结果.有小费吗?我为此搜寻了很多东西.

This is the only time I can get anything to show up. Any other link just produces nothing on the graph. Any tips? I have searched high and low for this.

layout = dict(title = 'Crime Cluster Statistics',
                  yaxis = dict(zeroline = False),
                  xaxis = dict(zeroline = False),
                  images= [dict(
                      source= "https://images.plot.ly/language-icons/api-home/python-logo.png",
                      xref= "x",
                      yref= "y",
                      x= 0,
                      y= 3,
                      sizex= 150000,
                      sizey= 150000,
                      sizing= "stretch")]
                 )

我真正想要的是将美国状态的图像拟合到图像的背景上,因为这些点应该代表该状态在GPS坐标下的事件.但是除了这个图像,我似乎无法将任何图像加载到背景上.

What I actually REALLY want is to fit an image of a US state on the background of the image, as the dots are supposed to represent events at a GPS coordinate for that state. But I can't seem to get any image to load onto the background besides this one.

推荐答案

我也尝试执行此操作(读取本地图像并将其作为背景绘制在绘图中),并不断地四处浏览(可能因为我对整个图像还很陌生,并且还试图了解base64编码).这是我在Jupyter笔记本中使用密谋离线的简单解决方案:

I was trying to do this as well (read a local image and make it my background in a plotly graph) and kept going around and around with it (likely because I am pretty new to the whole image thing and was also trying to understand base64 encoding). This is my simple solution in a Jupyter notebook using plotly offline:

import base64
with open("C:/My.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

现在,可打印图形的布局应如下所示(在这种情况下-请记住,您正在使用图形中的实际数据范围在其上放置图像,因此x和Y必须位于适当的范围):

Now your layout for the plotly graph can look like this (in this case - keep in mind that you are using the actual data ranges in the graph to place the image on it, so the x and Y have to be in the appropriate range):

"layout": Layout(title='Graph Title', 
                 yaxis=dict(title='Y Label'), 
                 xaxis=dict(title='X Label'),
                 images=[dict(
                  source= encoded_image,
                  xref= "x",
                  yref= "y",
                  x= 0,
                  y= 10,
                  sizex= 20,
                  sizey= 10,
                  sizing= "stretch",
                  opacity= 0.7,
                  layer= "below")])

这篇关于阴谋的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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