Python -在matplotlib中与极散点图的坐标变换。

时间:2022-09-10 22:26:47

I have to associate an HTML clickable map on some polar plots generated with matplotlib, so I need a method to convert the Data coordinates to the pixel coordinates of the figure.

我必须将一个HTML可点击映射与matplotlib生成的一些极坐标图相关联,因此我需要一个方法来将数据坐标转换为图形的像素坐标。

The code, adapted from How to get pixel coordinates for Matplotlib-generated scatterplot?, is:

这段代码是根据如何获得matplotlib生成的散点图的像素坐标而改编的?是:

# 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))

which gives

这给了

328.00  240.00
354.80  207.69
381.60  175.38
408.40  143.06
435.20  110.75
461.99  78.44

Only the first point, placed in the center of the graph, is correct. The other ones appear to be shifted, as if the figure were stretched horizontally. This figure represents the points as drawn by matplotlib (red) and the ones I drew following the coordinates:

只有放在图中心的第一点是正确的。其他的似乎被移动了,好像这个数字是水平拉伸的。这个图表示由matplotlib(红色)绘制的点,以及我在坐标下绘制的点:

Python -在matplotlib中与极散点图的坐标变换。

What could be going on? Thanks for your help!

到底发生了什么?谢谢你的帮助!

Update 03 Aug 2017

2017年8月3日更新

The "stretching" is even weirder: I tried to plot an octahedron inscribed in a circle of radius 3 with this command:

“拉伸”甚至更奇怪:我试图用这个命令画出一个半径为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')

and the same routine gave as a result

结果也一样。

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

While the 4 points along the x and y axes are placed correctly, the other 4 corresponding to i*pi/2 + pi/4 angles (where i=0,1,2,3) suffer from the "stretching". Indeed this can be seen even by looking at their coordinates: for i=0,2,4,6 it should be true that |x[(i+4)%8]-x[i]| = |y[(i+4)%8]-y[i]|, while this does not appear to be the case.

当x轴和y轴上的4点被正确放置时,另4点对应于i*pi/2 + /4角(i=0,1,2,3)受到“拉伸”的影响。实际上,这可以通过观察它们的坐标来观察:对于i=0、2、4、6,它应该是真的,|x[(i+4)%8]-x[i]| = |y[(i+4)%8]-y[i]|,而这似乎并不是这样。

1 个解决方案

#1


0  

This is a very tricky problem, turns out it's already been solved here:

这是一个非常棘手的问题,它已经在这里得到了解决:

Inconsistent figure coordinates in matplotlib with equal aspect ratio

在matplotlib中,不一致的图形坐标与等宽比。

The problem is that when setting the aspect to equal the dimensions and positions of the axes can only be determined by matplotlib once something is drawn onto the canvas. Before plotting the data, it cannot know where the axes would reside in the final figure.

问题是,当将相位设置为相等时,只有当物体被拉到画布上时,才能由matplotlib决定轴的尺寸和位置。在绘制数据之前,它不知道最终的图形中坐标轴的位置。

The easiest solution is to call

最简单的解决办法是打电话。

fig.canvas.draw()

right after the plot command but before doing any transformation works. In this way, the figure gets drawn to the canvas, applying the equal aspect; and from this point on, the correct transformations are available.

在plot命令之后,但是在进行任何转换之前。这样,图形就被绘制到画布上,应用了相同的方面;从这一点开始,正确的转换是可用的。

#1


0  

This is a very tricky problem, turns out it's already been solved here:

这是一个非常棘手的问题,它已经在这里得到了解决:

Inconsistent figure coordinates in matplotlib with equal aspect ratio

在matplotlib中,不一致的图形坐标与等宽比。

The problem is that when setting the aspect to equal the dimensions and positions of the axes can only be determined by matplotlib once something is drawn onto the canvas. Before plotting the data, it cannot know where the axes would reside in the final figure.

问题是,当将相位设置为相等时,只有当物体被拉到画布上时,才能由matplotlib决定轴的尺寸和位置。在绘制数据之前,它不知道最终的图形中坐标轴的位置。

The easiest solution is to call

最简单的解决办法是打电话。

fig.canvas.draw()

right after the plot command but before doing any transformation works. In this way, the figure gets drawn to the canvas, applying the equal aspect; and from this point on, the correct transformations are available.

在plot命令之后,但是在进行任何转换之前。这样,图形就被绘制到画布上,应用了相同的方面;从这一点开始,正确的转换是可用的。