[英]how to find pixel coordinates of an image for plotly scatter plot?
how to find pixel coordinates of an image for plotly scatter plot?如何找到 plotly 散射 plot 的图像的像素坐标?
What I am trying to achieve?我想要达到什么目的? :: Scatter plot on top of the metro map with the origin and destination passenger count in animated (hourly basis)
:: 将 plot 分散在地铁 map 顶部,以动画形式显示出发地和目的地的乘客数量(每小时)
Issue:: To find the location of each station i have used the "mpld3" to know the pixel location "plt.scatter([365], [824])" you have to manually find the exact location by adjusting these two values.问题:: 要找到每个站点的位置,我使用“mpld3”来了解像素位置“plt.scatter([365], [824])”,您必须通过调整这两个值手动找到确切位置。 But when I use the same values on the ploty scatter plot it's showing a different place.
但是当我在 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]
To find the pixel in image using the below script (output is not working in the ploty)使用以下脚本查找图像中的像素(输出在 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))
Kindly let me if any have better solution.如果有更好的解决方案,请告诉我。
In case I understand it correctly here is solution: 1st option: set the size of original image and plot to be the same, or 2nd option: in case you don't want to change the size adjust the coordinates to fit the plot area如果我理解正确,这里是解决方案:第一个选项:将原始图像和 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)
this will move the point to the correct place.这会将点移动到正确的位置。 Hopefully this will help.
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.