背景图片来自Google Drive in plotly

Background image from Google Drive in plotly

我正在创建一个 Google colab,其中包含图像上热图的一些数据可视化。我一直在使用类似于 plotly website.

中的代码

它在网站上的 link 上工作正常,但是当尝试使用 Google 驱动器 link 时,它不起作用。我正在使用这种格式的 link:https://docs.google.com/uc?export=download&id=<file-id> 在降价字段中使用图像时图像显示正常,但不是通过 plotly。

代码示例:

fig = px.density_heatmap(df, x="x", y="y",
                         color_continuous_scale = [(0, "rgba(0, 255, 0, 0)"), 
                                                   (0.5, "rgba(0, 255, 0, 0.5)"), 
                                                   (1, "rgba(0, 255, 0, 1)")],
                         height=810, width=1440)
fig.add_layout_image(
        dict(
            source="https://docs.google.com/uc?export=download&id=1JEyyYCse6sWM3yMUwDG2l7XzaGxej-VZ",
            xref="x",
            yref="y",
            x=0,
            y=0,
            sizex=1920,
            sizey=1080,
            sizing="stretch",
            opacity=1,
            layer="below")
)
fig.update_layout(template="plotly_white")
fig.show()

是否有解决方案,或者这只是图书馆没有考虑极端情况的情况?

编辑:我正在寻找使用未公开的 Google 驱动器 Link,希望我可以使用 Google Colab 凭据。找到了 here

的可能答案

你有URL提示下载图片。如 plotly documentation 中所述,您需要图像的 URL 或 PIL 图像对象。

A background image can be added to the layout of a figure with fig.add_layout_image or by setting the images parameter of go.Layout. The source attribute of a go.layout.Image can be the URL of an image, or a PIL Image object (from PIL import Image; img = Image.open('filename.png')).

也就是说,您可以下载图像并将其作为 PIL 图像对象读取到 python 中,然后将其用作背景。

见下例;由于您还没有共享您的数据,我正在使用 plotly 网页上的示例数据集。

import requests
from PIL import Image
import plotly.graph_objects as go

正在下载图片

## function to dowload google drive image taken from 
def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)    

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value

    return None

def save_response_content(response, destination):
    CHUNK_SIZE = 32768

    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk: ## filter out keep-alive new chunks
                f.write(chunk)

if __name__ == "__main__":
    file_id = 'TAKE ID FROM SHAREABLE LINK'
    destination = 'DESTINATION FILE ON YOUR DISK'
    download_file_from_google_drive(file_id, destination)
    
## download the image
download_file_from_google_drive("1JEyyYCse6sWM3yMUwDG2l7XzaGxej-VZ", "D:\test\gdrive.png")

图表

## read the image into python as a PIL Image object
img = Image.open("D:\test\gdrive.png")


## Create figure
fig = go.Figure()

## Add trace
fig.add_trace(
    go.Scatter(x=[0, 0.5, 1, 2, 2.2], y=[1.23, 2.5, 0.42, 3, 1])
)

## Add images
fig.add_layout_image(
        dict(
            source=img,
            xref="x",
            yref="y",
            x=0,
            y=3,
            sizex=3,
            sizey=3,
            sizing="stretch",
            opacity=0.5,
            layer="below")
)

## Set templates
fig.update_layout(template="plotly_white")

fig.show()