簡體   English   中英

如何找到 plotly 散射 plot 的圖像的像素坐標?

[英]how to find pixel coordinates of an image for plotly scatter plot?

如何找到 plotly 散射 plot 的圖像的像素坐標?

我想要達到什么目的? :: 將 plot 分散在地鐵 map 頂部,以動畫形式顯示出發地和目的地的乘客數量(每小時)

問題:: 要找到每個站點的位置,我使用“mpld3”來了解像素位置“plt.scatter([365], [824])”,您必須通過調整這兩個值手動找到確切位置。 但是當我在 ploty scatter plot 上使用相同的值時,它顯示了不同的位置。

def animate_stations(df, x_col, y_col, animation_frame, size, hover_name, title):

    init_notebook_mode()
    fig = px.scatter(df, 
                 x=x_col,
                 y=y_col, 
                 animation_frame= animation_frame, 
                 size= size, 
                 hover_name = hover_name,
                 range_x=(0,2050), 
                 range_y=(2050,0), 
                 width=700, 
                 height=700,
                 labels = {'origin_x':'', 'origin_y':''})
    image_filename = 'metro.jpg'
    plotly_logo = base64.b64encode(open(image_filename, 'rb').read())
    
    fig.update_layout(xaxis_showgrid=False, 
                    yaxis_showgrid=False,
                    xaxis_showticklabels=False,
                    yaxis_showticklabels=False,
                    title= title,
                    images= [dict(
                    source='data:image/jog;base64,{}'.format(plotly_logo.decode()),
                    xref="paper", yref="paper",
                    x=0, y=1,
                    sizex=1, sizey=1,
                    xanchor="left",
                    yanchor="top",
                    sizing="stretch",
                    layer="below")])
    iplot(fig)
    
station_coords = {
    'Mall of the Emirates Metro Station':[934, 935]
}    

def get_x_coord(station):
    return station_coords[station][0]

def get_y_coord(station):
    return station_coords[station][1]

在此處輸入圖像描述

使用以下腳本查找圖像中的像素(輸出在 ploty 中不起作用)

import matplotlib.pyplot as plt 
import mpld3  
from mpld3 import plugins
img = plt.imread("metro.jpg") 
fig, ax = plt.subplots(figsize=(14,14))
ax.imshow(img) 
plt.grid('on')
plt.axis('off') 
plt.scatter([365], [824]) 
plugins.connect(fig, plugins.MousePosition(fontsize=14))

在此處輸入圖像描述

如果有更好的解決方案,請告訴我。

如果我理解正確,這里是解決方案:第一個選項:將原始圖像和 plot 的大小設置為相同,或者第二個選項:如果您不想更改大小,請調整坐標以適合 plot 區域

new_x_coord = int((old_x_coord/old_x_len)*new_x_len)
new_y_coord = int((old_y_coord/old_y_len)*old_y_len)

這會將點移動到正確的位置。 希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM