【发布时间】:2017-07-31 23:34:59
【问题描述】:
我必须在一些用matplotlib生成的极坐标图上关联一个HTML可点击地图,所以我需要一种方法来将Data坐标转换为图形的像素坐标。
代码,改编自How to get pixel coordinates for Matplotlib-generated scatterplot?,为:
# Creates 6 points on a ray of the polar plot
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
points, = ax.plot([1,1,1,1,1,1],[0,1,2,3,4,5], 'ro')
ax.set_rmax(5)
x, y = points.get_data()
xy_pixels = ax.transData.transform(np.vstack([x,y]).T)
xpix, ypix = xy_pixels.T
width, height = fig.canvas.get_width_height()
ypix = height - ypix
for xp, yp in zip(xpix, ypix):
print('{x:0.2f}\t{y:0.2f}'.format(x=xp, y=yp))
给了
328.00 240.00
354.80 207.69
381.60 175.38
408.40 143.06
435.20 110.75
461.99 78.44
只有位于图表中心的第一个点是正确的。其他的似乎移动了,就好像人物被水平拉伸了一样。 此图表示 matplotlib 绘制的点(红色)和我在坐标后绘制的点:
会发生什么? 感谢您的帮助!
2017 年 8 月 3 日更新
“拉伸”更奇怪:我尝试使用以下命令绘制一个内接在半径为 3 的圆中的八面体:
points, = ax.plot([math.pi/4,math.pi/2,3*math.pi/4,math.pi,5*math.pi/4,3*math.pi/2,7*math.pi/4,2*math.pi],[3,3,3,3,3,3,3,3], 'ro')
结果是相同的例程
495.01 110.70
328.00 57.14
160.99 110.70
91.81 240.00
160.99 369.30
328.00 422.86
495.01 369.30
564.19 240.00
虽然 x 和 y 轴上的 4 个点放置正确,但对应于 i*pi/2 + pi/4 角度(其中 i=0,1,2,3)的其他 4 个点会受到“拉伸”的影响.实际上,即使通过查看它们的坐标也可以看出这一点:对于 i=0,2,4,6,|x[(i+4)%8]-x[i]| 应该是真的= |y[(i+4)%8]-y[i]|,而事实并非如此。
【问题讨论】:
标签: python matplotlib coordinate-transformation polar-coordinates