Python 绘出背景图像

Python 绘出背景图像,python,background-image,plotly,Python,Background Image,Plotly,我正在尝试使用plotly在图形上获取背景图像。如果不使用images.plot.ly链接,我似乎无法显示任何图像。我在项目所在的文件夹中尝试了web URL和本地图像。这是我使用他们教程中的图像URL时显示的内容: 这是我唯一一次有机会出现。任何其他链接都不会在图形上产生任何结果。有什么建议吗?我到处寻找这个 layout = dict(title = 'Crime Cluster Statistics', yaxis = dict(zeroline =

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

这是我唯一一次有机会出现。任何其他链接都不会在图形上产生任何结果。有什么建议吗?我到处寻找这个

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坐标上的事件。但除了这张图片外,我似乎无法将任何图片加载到背景上。

我也在尝试这样做(读取一张本地图片,并将其作为绘图图中的背景),并不断地使用它(可能是因为我对整个图像非常陌生,并且还试图理解base64编码)。这是我在Jupyter笔记本中使用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
现在,plotly图形的布局可以如下所示(在这种情况下-请记住,您使用图形中的实际数据范围来放置图像,因此x和Y必须在适当的范围内):


如果这有助于其他人寻找如何使本地图像作为绘图背景工作,以下是我用来使其工作的方法:

figure = {'data': data,
          'layout': {'xaxis': {'range': [-800, 800], 'autorange': False, 'dtick':100, 'title':'South'},
                     'yaxis': {'range': [-800, 800], 'autorange': False, 'dtick':100,'title': 'West (km)'},
                     'title': 'Sample LEO1 Main-Lobe Ground Contour (4.5 deg)',
                     'autosize':False, 'width':1.5*437, 'height':1.5*615,
                     'images': [{'source': "C:\\map.jpg",                          
                                 'sizing': 'stretch', 'xref': 'paper', 'yref': 'paper', 'x':0,'y':1, 'layer':'below',
                                 'sizex':1,'sizey':1,'opacity':1
                                }]                     
                    },                                        
         }
。。。其中“数据”以前设置为图形对象

layer=“below”确实帮助了我。
figure = {'data': data,
          'layout': {'xaxis': {'range': [-800, 800], 'autorange': False, 'dtick':100, 'title':'South'},
                     'yaxis': {'range': [-800, 800], 'autorange': False, 'dtick':100,'title': 'West (km)'},
                     'title': 'Sample LEO1 Main-Lobe Ground Contour (4.5 deg)',
                     'autosize':False, 'width':1.5*437, 'height':1.5*615,
                     'images': [{'source': "C:\\map.jpg",                          
                                 'sizing': 'stretch', 'xref': 'paper', 'yref': 'paper', 'x':0,'y':1, 'layer':'below',
                                 'sizex':1,'sizey':1,'opacity':1
                                }]                     
                    },                                        
         }