为什么 matplotlib ax.transData.transform 在不同的 ipython 单元格中给出不同的值?如何解决这个问题? [英] Why does matplotlib ax.transData.transform gives different values in different ipython cells? How to fix this?

查看:30
本文介绍了为什么 matplotlib ax.transData.transform 在不同的 ipython 单元格中给出不同的值?如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ipython 中遇到了 matplotlib 的 ax.transData.transform 一个非常奇怪的问题.基本上,在声明 fig, ax = plt.subplots() 的单元格中,为 xy 轴不正确.但是,如果您在后面的单元格中运行 ax.transData.transform,它会返回正确的值.

I was suffering a really bizarre issue with matplotlib's ax.transData.transform in ipython. Basically, in the cell where fig, ax = plt.subplots() is declared, running ax.transData.transform for either the x or y axis is incorrect. However, if you run ax.transData.transform in a later cell, it returns the correct values.

为了演示,这里是在声明 fig, ax = plt.subplots() 的单元格中运行转换的代码:

To demonstrate, here is code running the transformation in the cell where fig, ax = plt.subplots() is declared:

fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.set_ylim(-2,2)
ax.set_xlim(-5,5)
nsize=[x*100 for x in d]
yradius = (ax.transData.transform([(0,2)]) - ax.transData.transform([(0,1)]))[0,1]
print("yRadius: {}".format(yradius))
xradius = (ax.transData.transform([(2,0)]) - ax.transData.transform([(1,0)]))[0,0]
print("xRadius: {}".format(xradius))

打印输出为

yRadius: 54.360000000000014
xRadius: 33.48000000000002

在上面单元格下方的单元格中,如果我运行

In a cell below the above cell, if I run

yradius = (ax.transData.transform([(0,2)]) - ax.transData.transform([(0,1)]))[0,1]
print("yRadius: {}".format(yradius))
xradius = (ax.transData.transform([(2,0)]) - ax.transData.transform([(1,0)]))[0,0]
print("xRadius: {}".format(xradius))

输出为

yRadius: 33.48000000000002
xRadius: 33.48000000000002

为什么两个单元格的值不一样?尽管第一个单元格的 xRadius 与底部单元格的值匹配,但 yRadius 已关闭.我认为底部单元格的值是正确的,因为由于 ax 方面设置为相等,所以两个半径应该相同.为什么这里第一个单元格的 yRadius 是错误的?在为 xlimylim 尝试了一堆不同的值后,我发现第一个单元格中两个半径的最小值是正确的,而另一个是关闭的.在我看来,这是一个非常奇怪的错误.有没有办法让它在初始单元格中返回正确的值?

Why are the values of the two cells not the same? Although the xRadius of the first cell matches the values of the bottom cell, the yRadius is off. I think the bottom cell's values are correct, because since the ax aspect is set to be equal, the two radii should be the same. Why is the yRadius of the first cell wrong here? After trying out a bunch of different values for xlim and ylim I found the pattern that the minimum of the two radii in the first cell is correct, while the other one is off. This seems to me a very strange error. Is there any way of making it return the correct values in the initial cell?

推荐答案

您的观察非常准确.发生的事情很简单,只有在绘制图形后才应用相等的方面.这是第二个单元格的情况(因为该图显示为第一个单元格的输出).然而,在第一个单元格中,轴仍然具有其原始范围.

Your observation is pretty accurate. What happens is simply that the equal aspect is only applied once the figure is drawn. This is the case for the second cell (because the figure is shown as output of the first). However in the first cell the axes still has its original extent.

解决方案:在尝试从中获取任何坐标之前先绘制图形,

Solution: Draw the figure before trying to obtain any coordinates from it,

fig.canvas.draw()

# Now obtain coordinates:
yradius = (ax.transData.transform([(0,2)]) - ax.transData.transform([(0,1)]))[0,1]
print("yRadius: {}".format(yradius))
xradius = (ax.transData.transform([(2,0)]) - ax.transData.transform([(1,0)]))[0,0]
print("xRadius: {}".format(xradius))

这篇关于为什么 matplotlib ax.transData.transform 在不同的 ipython 单元格中给出不同的值?如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆