Skip to content

Loading…

facecolor of patches by FITSFigure.show_{circles,ellipses,rectangles,polygons} is always None #84

Closed
monodera opened this Issue · 14 comments

3 participants

@monodera

When I overlay, for example, circles on an image by using FITSFigure.show_circles() with facecolor='none' (i.e., no color), it always uses the default color set in ~/.matplotlib/matplotlirc ('blue' in my case), which probably means the function automatically change the input facecolor to facecolor=None.

I tried to fix the problem by looking at the code, and I found it works for me when I change aplpy/aplpy.py as follows.

--- aplpy.py.org    2012-09-07 18:08:40.000000000 +0200
+++ aplpy.py        2012-09-07 18:09:18.000000000 +0200
@@ -1032,9 +1032,9 @@

         patches = []
         for i in range(len(xp)):
-            patches.append(Circle((xp[i], yp[i]), radius=rp[i], **kwargs))
+            patches.append(Circle((xp[i], yp[i]), radius=rp[i]))

-        p = PatchCollection(patches, match_original=True)
+        p = PatchCollection(patches, match_original=False, **kwargs)
         if zorder is not None:
             p.zorder = zorder
         c = self._ax1.add_collection(p)

If this is right way to do, other functions, show_rectangles, show_ellipses, and show_polygons, should be fixed accordingly as I have the same problem with them. I'm still not sure this is right way for everybody, but at least works for me and want somebody else to check the issue.

@astrofrog
APLpy member

@mojin - this sounds like an old matplotlib bug that was later fixed. Could you do:

import matplotlib
print matplotlib.__version__

? Thanks!

@monodera
>>> import matplotlib
>>> print matplotlib.__version__
1.2.x

I've installed dev version of matplotlib, which I wanted to use updated postscript implementation.

@astrofrog
APLpy member

Interesting - I'll look into it!

@astrofrog
APLpy member

@mojin - what color circle (edge and face) does the following script produce?

import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
c = Circle((0.5, 0.5), radius=0.2, facecolor='none')
coll = PatchCollection([c], match_original=True)
ax.add_collection(coll)
fig.savefig('test.png')
@monodera

This produce a circle with an edge with black line, but not filled with any color. I understand this is what you expected. There might be something wrong in my script with aplpy. Could you please give an example with aplpy?

@astrofrog
APLpy member

Here's an APLpy example:

import aplpy

f = aplpy.FITSFigure('2MASS_k.fits.gz')
f.show_grayscale()
f.show_circles([266.41], [-29.], [0.03], facecolor='none', edgecolor='red')
f.show_circles([266.41], [-29.], [0.06], facecolor='none', edgecolor='green')
f.show_circles([266.41], [-29.], [0.09], facecolor='none', edgecolor='blue')
f.save('test.png')

The FITS file is here and the output is:

result

Do you get the same?

@monodera

I got the same result!
However, when I saved it as pdf (f.save('test.pdf')), I got strange one like this: https://www.dropbox.com/s/ap54w6nt5p53498/apltest_circle1.pdf

@astrofrog
APLpy member

Ah, interesting - so what do you get for

import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
c = Circle((0.5, 0.5), radius=0.2, facecolor='none')
coll = PatchCollection([c], match_original=True)
ax.add_collection(coll)
ax.patch.set_facecolor('gray')
fig.savefig('test.pdf')

(i.e. as before, but saving as a PDF, and setting the background of the axes to gray). I have matplotlib 1.1.0 right now, so it could be an old bug that creeped back in.

@astrofrog
APLpy member

Ok, that's interesting - I'll investigate some more.

@wkerzendorf

Hi @astrofrog, just had the same problem.

@astrofrog
APLpy member

@wkerzendorf - what version of matplotlib are you using?

@wkerzendorf

@astrofrog oh actually a development version 1.3.x. So interestingly enough, that works:

imshow(random.random((100,100)))
circle = matplotlib.patches.Circle((50, 50), 10, facecolor='none', edgecolor='blue', linewidth=3)
ax = gca()
ax.add_patch(circle)
savefig('test.pdf')
@astrofrog astrofrog added a commit that closed this issue
@astrofrog astrofrog Add workaround for Matplotlib 1.2.x bug related to the facecolor of p…
…atches for PDF output. Closes #84.
6d08bf1
@astrofrog astrofrog closed this in 6d08bf1
@astrofrog
APLpy member

@mojin @wkerzendorf - sorry for the delay, the latest developer version of APLpy now includes a workaround (6d08bf1) for this Matplotlib bug (matplotlib/matplotlib#1860).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.