transData.transform on 'log' plot

Hello,

When I try to use transData.transform on a log plot, an error occurs :
...
File "E:\Python26\lib\site-packages\matplotlib\transforms.py", line 1895, in transform
     self._a.transform(points))
   File "E:\Python26\lib\site-packages\matplotlib\transforms.py", line 1723, in transform
     x_points = x.transform(points[:, 0])
TypeError: tuple indices must be integers, not tuple

Here is an example :
#!/usr/bin/env python

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure

import wx

class CanvasFrame(wx.Frame):

     def __init__(self):
         wx.Frame.__init__(self,None,-1,
                          'CanvasFrame',size=(550,350))

         self.SetBackgroundColour(wx.NamedColor("WHITE"))

         self.figure = Figure()

         self.axes = self.figure.add_subplot(111)
         self.axes.set_xscale('log')

         p1 = (1,1)
         p2 = (2,2)
         _x1, _y1 = self.axes.transData.transform(p1)
         _x2, _y2 = self.axes.transData.transform(p2)
         _xy = [[_x1, _y1], [_x2, _y2]]

         xy = self.axes.transData.inverted().transform(_xy)

         self.axes.plot(xy)
         self.canvas = FigureCanvas(self, -1, self.figure)

         self.sizer = wx.BoxSizer(wx.VERTICAL)
         self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
         self.SetSizer(self.sizer)
         self.Fit()

class App(wx.App):

     def OnInit(self):
         frame = CanvasFrame()
         frame.Show(True)

         return True

app = App(0)
app.MainLoop()

If I comment the line : self.axes.set_xscale('log'), it works fine.

python 2.6.4
wxpython 2.8.10
mpl 0.99

Thanks by advance for your help.
Cédrick

The `transform` method requires that its argument is a numpy array. You "got lucky" with duck typing in the non-log case, but there is an assumption in the transformation code that it can use Numpy array functionality on whatever is passed in. Unfortunately, the transforms code is so low-level and core to matplotlib that any checking or coercing of the types passed in makes a significant impact on interactive performance, so I'm reluctant to add that. The docstring does say this:

        """
        Performs the transformation on the given array of values.

        Accepts a numpy array of shape (N x :attr:`input_dims`) and
        returns a numpy array of shape (N x :attr:`output_dims`).
        """

I've attached a fixed version of your script that uses Numpy arrays rather than tuples for the points.

Mike

C�drick FAURY wrote:

log_transform.py (1.19 KB)

···

Hello,

When I try to use transData.transform on a log plot, an error occurs :
...
File "E:\Python26\lib\site-packages\matplotlib\transforms.py", line 1895, in transform
     self._a.transform(points))
   File "E:\Python26\lib\site-packages\matplotlib\transforms.py", line 1723, in transform
     x_points = x.transform(points[:, 0])
TypeError: tuple indices must be integers, not tuple

Here is an example :
#!/usr/bin/env python

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure

import wx

class CanvasFrame(wx.Frame):

     def __init__(self):
         wx.Frame.__init__(self,None,-1,
                          'CanvasFrame',size=(550,350))

         self.SetBackgroundColour(wx.NamedColor("WHITE"))

         self.figure = Figure()

         self.axes = self.figure.add_subplot(111)
         self.axes.set_xscale('log')

         p1 = (1,1)
         p2 = (2,2)
         _x1, _y1 = self.axes.transData.transform(p1)
         _x2, _y2 = self.axes.transData.transform(p2)
         _xy = [[_x1, _y1], [_x2, _y2]]

         xy = self.axes.transData.inverted().transform(_xy)

         self.axes.plot(xy)
         self.canvas = FigureCanvas(self, -1, self.figure)

         self.sizer = wx.BoxSizer(wx.VERTICAL)
         self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
         self.SetSizer(self.sizer)
         self.Fit()

class App(wx.App):

     def OnInit(self):
         frame = CanvasFrame()
         frame.Show(True)

         return True

app = App(0)
app.MainLoop()

If I comment the line : self.axes.set_xscale('log'), it works fine.

python 2.6.4
wxpython 2.8.10
mpl 0.99

Thanks by advance for your help.
C�drick

------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options
  
--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA