带有log轴的`transform_non_affine`中的异常

时间:2014-11-16 19:58:23

标签: python-3.x matplotlib

当我在绘制对数刻度时尝试使用axes.transData时,我遇到了一个奇怪的错误。重现此错误的最小代码:

#!/usr/bin/env python3
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

fig = Figure(figsize=(8,6))
canvas = FigureCanvas(fig)
ax = fig.add_subplot(1,1,1)
ax.plot(range(10))
ax.set_yscale('log')                 # <--- works fine without this line
print(ax.transData.transform((1,1))) # <--- exception thrown here
canvas.print_figure('test.pdf')

堆栈跟踪如下:

  File "/usr/local/lib/python3.3/site-packages/matplotlib-1.3.1-py3.3-linux-x86_64.egg/matplotlib/transforms.py", line 1273, in transform
    return self.transform_affine(self.transform_non_affine(values))
  File "/usr/local/lib/python3.3/site-packages/matplotlib-1.3.1-py3.3-linux-x86_64.egg/matplotlib/transforms.py", line 2217, in transform_non_affine
    return self._a.transform_non_affine(points)
  File "/usr/local/lib/python3.3/site-packages/matplotlib-1.3.1-py3.3-linux-x86_64.egg/matplotlib/transforms.py", line 2002, in transform_non_affine
    x_points = x.transform_non_affine(points)[:, 0:1]
TypeError: tuple indices must be integers, not tuple

如果我注释掉set_yscale('log')它运行正常。有谁知道为什么这个转换不起作用?

1 个答案:

答案 0 :(得分:1)

不完全令人满意,但我找到了解决方法。该问题似乎与transform的1维数组输入有关。奇怪的是,如果我使用它,它会起作用:

ax.transData.transform(pts[None,:])

换句话说,我必须重塑数组使其成为二维。