剧情背景图片

2022-01-21 00:00:00 python plotly background-image

问题描述

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

我真正想要的是在图像的背景上放置一张美国州的图像,因为这些点应该代表该州 GPS 坐标处的事件.但除了这张之外,我似乎无法将任何图像加载到背景中.

解决方案

我也在尝试这样做(读取本地图像并将其作为绘图图表中的背景)并一直使用它(可能因为我对整个图像非常陌生,并且还试图理解 base64 编码).这是我在 Jupyter 笔记本中使用 plotly 离线的简单解决方案:

导入base64使用 open("C:/My.png", "rb") 作为 image_file:编码字符串 = base64.b64encode(image_file.read()).decode()#添加使用字符串作为源时需要的前缀编码图像=数据:图像/png;base64,"+编码字符串

现在您的绘图图布局可能如下所示(在这种情况下 - 请记住,您正在使用图中的实际数据范围来放置图像,因此 x 和 Y 必须在合适的范围):

"layout": Layout(title='Graph Title',yaxis=dict(title='Y 标签'),xaxis=dict(title='X 标签'),图像= [字典(源=编码图像,外部参照=x",yref = "y",x = 0,y = 10,大小 = 20,大小= 10,尺寸=拉伸",不透明度= 0.7,层=下面")])

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")]
                 )

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.

解决方案

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

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")])

相关文章