Polar plots rendered incorrectly in SVG

Dear all,

I have a problem with exporting polar plots to SVG. When attached to
axes which are not centered in the figure, the content (grids, data,
etc.) seems not to be shifted correctly with the axes. However, when I
plot it directly to the screen or export to PNG everything is fine.

Here is an example reproducing this error:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_axes([0.4, 0.4, 0.25, 0.25], polar=True)

theta1 = np.pi/4
ax.plot([theta1, theta1], [0, 1], '-')

plt.savefig('test_polar.svg')
plt.savefig('test_polar.png')
plt.show()

I was able to reproduce the problem it in matplotlib 0.99.0 (Backend:
GTKAgg).

Cheers,

Bartosz

Thanks for reporting the problem.
I can reproduce this error in the svn trunk.

My diagnosis is that this is because the clip mask is not correctly
set, i.e., the mask path is not properly flipped in the svg backend.
I was able to solve this particular problem using the attached patch.
But, i'm not sure if this patch is a general solution. So, I hope
other developers step in.

Bartosz, below is a workaround you may use meanwhile. Note that the
workaround will give correct result only for svg backend.

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_axes([0.4, 0.4, 0.25, 0.25], polar=True)

# modify the ax.patch transform to work around with svg backend bug
import matplotlib.transforms as mtransforms
flipped_transAxes = mtransforms.BboxTransformTo(ax.bbox) \
                    + mtransforms.Affine2D().scale(1.0,
-1.0).translate(0., 72*fig.get_figheight())

ax.patch.set_transform(flipped_transAxes)
ax.xaxis.set_clip_path(ax.patch)
ax.yaxis.set_clip_path(ax.patch)

theta1 = np.pi/4
ax.plot([theta1, theta1], [0, 1], '-')

plt.savefig('test_polar.svg')
plt.savefig('test_polar.png')

Regards,

-JJ

svg_clip.diff (1.37 KB)

···

On Wed, Sep 16, 2009 at 2:56 PM, Bartosz Telenczuk <b.telenczuk@...2786...> wrote:

Dear all,

I have a problem with exporting polar plots to SVG. When attached to
axes which are not centered in the figure, the content (grids, data,
etc.) seems not to be shifted correctly with the axes. However, when I
plot it directly to the screen or export to PNG everything is fine.

Here is an example reproducing this error:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_axes([0.4, 0.4, 0.25, 0.25], polar=True)

theta1 = np.pi/4
ax.plot([theta1, theta1], [0, 1], '-')

plt.savefig('test_polar.svg')
plt.savefig('test_polar.png')
plt.show()

I was able to reproduce the problem it in matplotlib 0.99.0 (Backend:
GTKAgg).

Cheers,

Bartosz

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Thanks. I think you're right, Jae-Joon. I've committed a slightly simplified version of your patch on the 0.99 branch and trunk.

Mike

Jae-Joon Lee wrote:

···

Thanks for reporting the problem.
I can reproduce this error in the svn trunk.

My diagnosis is that this is because the clip mask is not correctly
set, i.e., the mask path is not properly flipped in the svg backend.
I was able to solve this particular problem using the attached patch.
But, i'm not sure if this patch is a general solution. So, I hope
other developers step in.

Bartosz, below is a workaround you may use meanwhile. Note that the
workaround will give correct result only for svg backend.

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_axes([0.4, 0.4, 0.25, 0.25], polar=True)

# modify the ax.patch transform to work around with svg backend bug
import matplotlib.transforms as mtransforms
flipped_transAxes = mtransforms.BboxTransformTo(ax.bbox) \
                    + mtransforms.Affine2D().scale(1.0,
-1.0).translate(0., 72*fig.get_figheight())

ax.patch.set_transform(flipped_transAxes)
ax.xaxis.set_clip_path(ax.patch)
ax.yaxis.set_clip_path(ax.patch)

theta1 = np.pi/4
ax.plot([theta1, theta1], [0, 1], '-')

plt.savefig('test_polar.svg')
plt.savefig('test_polar.png')

Regards,

-JJ

On Wed, Sep 16, 2009 at 2:56 PM, Bartosz Telenczuk > <b.telenczuk@...2786...> wrote:
  

Dear all,

I have a problem with exporting polar plots to SVG. When attached to
axes which are not centered in the figure, the content (grids, data,
etc.) seems not to be shifted correctly with the axes. However, when I
plot it directly to the screen or export to PNG everything is fine.

Here is an example reproducing this error:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_axes([0.4, 0.4, 0.25, 0.25], polar=True)

theta1 = np.pi/4
ax.plot([theta1, theta1], [0, 1], '-')

plt.savefig('test_polar.svg')
plt.savefig('test_polar.png')
plt.show()

I was able to reproduce the problem it in matplotlib 0.99.0 (Backend:
GTKAgg).

Cheers,

Bartosz

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

    ------------------------------------------------------------------------

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
------------------------------------------------------------------------

_______________________________________________
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

Dear Jae-Joon,

Your workaround worked perfectly! Thanks a lot!

Cheers,

Bartosz