繁体   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