plot_surface fails with log axes ?

Hello,

i encountered a problem when trying to set axes as logarithmic in a 3D
plot using plot_surface().

I do not know if it is a known bug or if it is a workaround ...?
the failing code is :
#!/usr/bin/python
import os
import sys
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import axes3d
import numpy as np
# parameters we bach on
X = np.arange(0.1, 20, 0.5)
Y = np.arange(0.1, 20, 0.5)
X, Y = np.meshgrid(X, Y)
R = np.sqrt((X-10)**2 + (Y-10)**2)
Z = np.sin(R)

fig = figure(num=None, figsize=(6, 6), dpi=80, facecolor='#F2F2F2',
edgecolor='k')
ax = subplot(111,projection='3d')

# the two following make the plot fail
#ax.set_yscale('log')
ax.set_xscale('log')

ax.plot_surface(X,Y,Z, rstride=1, cstride=1, cmap=cm.jet, linewidths=.5)

show()

and the complete output is :
Traceback (most recent call last):
  File
"/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 395, in expose_event
    self._render_figure(self._pixmap, w, h)
  File
"/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
    FigureCanvasAgg.draw(self)
  File
"/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 401, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/figure.py",
line 884, in draw
    func(*args)
  File
"/usr/local/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py",
line 210, in draw
    ax.draw(renderer)
  File
"/usr/local/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axis3d.py",
line 393, in draw
    tick.draw(renderer)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/axis.py", line
234, in draw
    self.label1.draw(renderer)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/text.py", line
591, in draw
    ismath=ismath)
  File
"/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 142, in draw_text
    return self.draw_mathtext(gc, x, y, s, prop, angle)
  File
"/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 131, in draw_mathtext
    x = int(x) + ox
  File "/usr/local/lib/python2.7/site-packages/numpy/ma/core.py", line
3795, in __int__
    raise MaskError, 'Cannot convert masked element to a Python int.'
numpy.ma.core.MaskError: Cannot convert masked element to a Python int.
Traceback (most recent call last):
  File
"/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 395, in expose_event
    self._render_figure(self._pixmap, w, h)
  File
"/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
    FigureCanvasAgg.draw(self)
  File
"/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 401, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/figure.py",
line 884, in draw
    func(*args)
  File
"/usr/local/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py",
line 210, in draw
    ax.draw(renderer)
  File
"/usr/local/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axis3d.py",
line 393, in draw
    tick.draw(renderer)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/axis.py", line
234, in draw
    self.label1.draw(renderer)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/text.py", line
591, in draw
    ismath=ismath)
  File
"/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 142, in draw_text
    return self.draw_mathtext(gc, x, y, s, prop, angle)
  File
"/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 131, in draw_mathtext
    x = int(x) + ox
  File "/usr/local/lib/python2.7/site-packages/numpy/ma/core.py", line
3795, in __int__
    raise MaskError, 'Cannot convert masked element to a Python int.'
numpy.ma.core.MaskError: Cannot convert masked element to a Python int.

any insight will be welcomed !
Thanks

···

--
RHENOVIA - Arnaud LEGENDRE.

Yes, this is a known bug and it is a very tricky one to untangle. My only suggestion is to plot the function using linearized log values. So, maybe something like this:

#!/usr/bin/python

import os

import sys

from pylab import *

from mpl_toolkits.mplot3d import Axes3D

from mpl_toolkits.mplot3d import axes3d

import numpy as np

parameters we bach on

X = np.log10(np.arange(0.1, 20, 0.5))

Y = np.log10(np.arange(0.1, 20, 0.5))

X, Y = np.meshgrid(X, Y)

R = np.sqrt((10**X-10)2 + (10Y-10)**2)

Z = np.sin(R)

fig = figure(num=None, figsize=(6, 6), dpi=80, facecolor=‘#F2F2F2’,

edgecolor=‘k’)

ax = subplot(111,projection=‘3d’)

ax.plot_surface(X,Y,Z, rstride=1, cstride=1, cmap=cm.jet, linewidths=.5)

show()

Now, that won’t make the points evenly spaced, so you might want to do something like the following:

X = np.log10(np.logspace(-1, 1.3, 40))

I hope that helps!
Ben Root

···

On Tue, Dec 6, 2011 at 10:08 AM, Arnaud <arnaud.legendre@…3876…> wrote:

Hello,

i encountered a problem when trying to set axes as logarithmic in a 3D

plot using plot_surface().

I do not know if it is a known bug or if it is a workaround …?

thanks, of course, this work.
I will have to edit ticks labels ...

shall we expect a correction in near future ?

best,

···

--
RHENOVIA - Arnaud LEGENDRE.

Le mardi 06 décembre 2011 à 10:45 -0600, Benjamin Root a écrit :

On Tue, Dec 6, 2011 at 10:08 AM, Arnaud <arnaud.legendre@...3876...> > wrote:
        Hello,
        
        i encountered a problem when trying to set axes as logarithmic
        in a 3D
        plot using plot_surface().
        
        I do not know if it is a known bug or if it is a
        workaround ...?

Yes, this is a known bug and it is a very tricky one to untangle. My
only suggestion is to plot the function using linearized log values.
So, maybe something like this:

#!/usr/bin/python
import os
import sys
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import axes3d
import numpy as np
# parameters we bach on
X = np.log10(np.arange(0.1, 20, 0.5))
Y = np.log10(np.arange(0.1, 20, 0.5))
X, Y = np.meshgrid(X, Y)
R = np.sqrt((10**X-10)**2 + (10**Y-10)**2)
Z = np.sin(R)

fig = figure(num=None, figsize=(6, 6), dpi=80, facecolor='#F2F2F2',
edgecolor='k')
ax = subplot(111,projection='3d')
ax.plot_surface(X,Y,Z, rstride=1, cstride=1, cmap=cm.jet,
linewidths=.5)

show()

Now, that won't make the points evenly spaced, so you might want to do
something like the following:

X = np.log10(np.logspace(-1, 1.3, 40))

I hope that helps!
Ben Root

It is doubtful unless I have some sort of eureka moment. I have been tinkering with this problem on-and-off for a few months now and haven’t figure out a solution that doesn’t require a complete revamp of the mplot3d module and matplotlib’s projection module.

Ben Root

···

On Tue, Dec 6, 2011 at 11:15 AM, Arnaud <arnaud.legendre@…3876…> wrote:

thanks, of course, this work.

I will have to edit ticks labels …

shall we expect a correction in near future ?