【问题标题】:Background image from Google Drive in plotly来自 Google Drive 的背景图片
【发布时间】:2020-10-09 17:18:22
【问题描述】:

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

网站上的链接可以正常工作,但是当尝试使用 Google Drive 链接时,它不起作用。我正在使用这种格式的链接:https://docs.google.com/uc?export=download&id=<file-id> 在 markdown 字段中使用时,图像显示良好,但不能通过 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 Drive Link,希望可以使用 Google Colab 凭据。找到了一个可能的答案here

【问题讨论】:

    标签: python plotly google-colaboratory imageurl


    【解决方案1】:

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

    背景图片可以添加到图形的布局中 fig.add_layout_image 或通过设置 go.Layout 的 images 参数。 go.layout.Image 的 source 属性可以是图片的 URL, 或 PIL Image 对象(来自 PIL import Image; img = Image.open('filename.png')).

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

    见下面的例子;由于您尚未共享数据,因此我正在使用 plotly 网页上的示例数据集。

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

    正在下载 iamge

    ## function to dowload google drive image taken from https://stackoverflow.com/a/39225272/6461462
    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()
    

    【讨论】:

    • 这适用于指定的图像,但我希望尽可能不必下载图像。另外,由于某种原因,下载了另一个我没有工作的图像。不确定是因为文件类型还是其他原因。现在就检查一下。
    • 弄清楚为什么我的其他图像不起作用。该图像不公开。因此,当尝试下载它时,它不起作用。我可能可以通过谷歌驱动器连接它,但这会破坏问题的目的。如果愿意,我可以澄清这个问题。
    • @Alex_H 正确的做法是发布带有更新信息的新答案并链接到该线程。这样,您会得到更好的关注,并且避免在一个线程中提出多个问题,这与 How to Ask 教程背道而驰。干杯。
    • 稍微改写问题得到了答案,这意味着没有优雅的方式来获取图像。我会将您的答案标记为正确,因为它回答了最初的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多