Bir*_*man 2 python background-image plotly
我正在尝试使用 plotly 在我的图表上获取背景图像。如果不使用 images.plot.ly 链接之一,我似乎无法显示任何图像。我在与项目相同的文件夹中尝试了 Web URL 和本地图像。这是我使用他们教程中的图像 URL 时显示的内容:
https://plot.ly/python/images/
这是我唯一一次可以让任何东西出现。任何其他链接都不会在图表上产生任何内容。有小费吗?我为此进行了高低搜索。
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")]
)
Run Code Online (Sandbox Code Playgroud)
我真正想要的是在图像的背景上拟合美国州的图像,因为这些点应该代表该州 GPS 坐标处的事件。但我似乎无法将任何图像加载到除此之外的背景上。
小智 6
我也试图这样做(读取本地图像并将其作为绘图图形的背景)并继续使用它(可能是因为我对整个图像还很陌生,并且还试图了解 base64编码)。这是我在 Jupyter notebook 中使用 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
Run Code Online (Sandbox Code Playgroud)
现在,绘图图的布局可能如下所示(在这种情况下 - 请记住,您正在使用图中的实际数据范围将图像放置在其上,因此 x 和 Y 必须在适当的范围内) :
"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")])
Run Code Online (Sandbox Code Playgroud)