# [AstroPy] Problem creating all-sky projection with matplotlib

Matt Davis mrdavis@stsci....
Tue Sep 13 15:40:23 CDT 2011

```Hi Eduardo,

About your grid issue: just move the grid statement further down in the code. I think matplotlib often just plots things in the order given in the code, so putting the grid at the end makes it come out on top. Another option is to use the zorder keyword. Objects created with the largest zorder keyword are plotted last.

I tried your code with the ax.grid call on line 27, just before making the color bar, and I get the grids.

Best,

Matt Davis

On Sep 13, 2011, at 4:30 PM, Eduardo Bañados Torres wrote:

> Hi Matt,
>
> Thanks for your response!!  your explanation of why I was getting this behavior is very clear.
> So, I finally solved the problem (I think) thanks to your answer and the one posted by Joe Kington in Stackoverflow.
>
> This time I used pcolormesh instead of imshow and I got this beautiful (at least for me) image:
>
> http://i53.tinypic.com/2r6l5k6.png
>
> The only "problem" is that now I cannot show the "grid" (ax.grid(True) is not working), so if someone know how to add the grid, please let me know ;) (although this is just a detail)
>
> If this is useful for someone else, I attach the "corrected version" of my code (which produces the image above).
>
> Thanks again!
>
> =========================corrected version========================
>
> import numpy as np
> import matplotlib.pyplot as plt
> import matplotlib.backends.backend_agg
>
> #array between 0 and 360 deg
> #CAVEAT: it seems that is needed an array from -180 to 180, so is just a
> #shift in the coordinates
> RA = np.random.random(10000)*360-180
> #array between -45 and 90 degrees
> DEC= np.random.random(10000)*135-45
>
> fig = plt.Figure((10, 5))
>
> ax.set_xlabel('RA')
> ax.set_ylabel('DEC')
>
> ax.set_xticklabels(np.arange(30,331,30))
>
> #The ax.grid is not working though =/
> ax.grid(color='r', linestyle='-', linewidth=2)
>
> hist,xedges,yedges = np.histogram2d(DEC,RA,bins=[60,40],range=[[-90,90],[-180,180]])
>
>
> image = ax.pcolormesh(X,Y,hist)
>
> cb = fig.colorbar(image, orientation='horizontal')
> canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig)
> fig.canvas.print_figure("image4.png")
>
>
>
> 2011/9/13 Matt Davis <mrdavis@stsci.edu>
> Hi Eduardo,
>
> You are seeing this behavior because imshow works just by filling in rows of the image with rows of your array. In your case, your array is zeros in the bottom quarter and non-zero in the top three-quarters, so that's what you see in the resulting plot. Imshow *does not* account for the projection.
>
> I think you would prefer to use pcolor so that your data is mapped to the projection. See: http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.pcolor
>
> Best,
>
> Matt Davis
>
> On Sep 13, 2011, at 9:38 AM, Eduardo Bañados Torres wrote:
>
>> Hi all,
>>
>> I posted this question in Stackoverflow (http://stackoverflow.com/questions/7355497/curious-bad-behavior-creating-all-sky-projections-with-matplotlib) but I haven't get any answer so far, so I hope some of you can help me :-)
>>
>> In short, I am plotting a density all-sky plot using the molloweide projection.  I create objects with coordinates ranging from 0 to 360 deg in RA and from -45 to 90 deg in DEC, but the output I get is the following:
>>
>> image1.png -> http://i56.tinypic.com/24mu96s.png
>>
>> A plot which is OK in RA (0-360) but in DEC ranges only between -35 to 90, so I am missing 10 degrees in the south.
>>
>> But I would expect this image:
>>
>> image2.png -> http://oi53.tinypic.com/2yl1nch.jpg
>> A plot ranging between 0 to360 and -45 to 90 as it was defined
>>
>> I attach the self-contained code to produce these images, I hope someone can tell me if I am doing something wrong that I can't notice now  or misunderstanding something in the code or if there is a curious bug in matplotlib??
>>
>>
>> ############the self-contained example################
>> import numpy as np
>>
>> import matplotlib.pyplot as plt
>> import matplotlib.backends.backend_agg
>>
>>
>> from math import pi
>>
>> #array between 0 and 360 deg
>> RA = np.random.random(10000)*360
>>
>>
>> #array between -45 and 90 degrees. By construction!
>> DEC= np.random.random(10000)*135-45
>>
>>
>>
>> fig = plt.Figure((10, 5))
>>
>>
>> ax.grid(True)
>> ax.set_xlabel('RA')
>>
>> ax.set_ylabel('DEC')
>>
>> ax.set_xticklabels(np.arange(30,331,30))
>>
>>
>> hist,xedges,yedges = np.histogram2d(DEC,RA,bins=[90,180],range=[[-90,90],[0,360]])
>>
>>
>> #TO RECOVER THE EXPECTED BEHAVIOUR (image2.png), I HAVE TO CHANGE -90 FOR -80 IN THE PREVIOUS LINE:
>> #hist,xedges,yedges = np.histogram2d(DEC,RA,bins=[90,180],range=[[-80,90],[0,360]])
>>
>>
>> #I DO NOT WHY!
>>
>> extent = (-pi,pi,-pi/2.,pi/2.)
>>
>>
>> image = ax.imshow(hist,extent=extent,clip_on=False,aspect=0.5,origin='lower')
>>
>>
>>
>> cb = fig.colorbar(image, orientation='horizontal')
>>
>>
>> canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig)
>>
>>
>>
>> fig.canvas.print_figure("image1.png")
>>
>> ######################################################
>>
>> Thanks,
>>
>>
>>
>> --
>>
>> _______________________________________________
>> AstroPy mailing list
>> AstroPy@scipy.org
>> http://mail.scipy.org/mailman/listinfo/astropy
>
>
>
>
> --