<div dir="ltr">Thanks you very much again Matt,<div><br></div><div>All the best,<br><br><div class="gmail_quote">2011/9/13 Matt Davis <span dir="ltr">&lt;<a href="mailto:mrdavis@stsci.edu">mrdavis@stsci.edu</a>&gt;</span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div style="word-wrap:break-word"><div>Hi Eduardo,</div><div><br></div><div>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.</div>
<div><br></div><div>I tried your code with the ax.grid call on line 27, just before making the color bar, and I get the grids.</div><div><br></div><div>Best,</div><div><br></div><font color="#888888"><div>Matt Davis</div>
</font><div><div></div><div class="h5"><br><div><div>On Sep 13, 2011, at 4:30 PM, Eduardo Bañados Torres wrote:</div><br><blockquote type="cite"><div dir="ltr">Hi Matt,<div><br></div><div>Thanks for your response!!  your explanation of why I was getting this behavior is very clear.</div>
<div>So, I finally solved the problem (I think) thanks to your answer and the one posted by Joe Kington in Stackoverflow.</div>
<div><span style="color:rgb(68, 68, 68);font-family:Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif;font-size:13px;line-height:17px;background-color:rgb(255, 255, 255)"><br>
</span></div><div><span style="background-color:rgb(255, 255, 255)">This time I used pcolormesh instead of imshow and I got this beautiful (at least for me) image:</span></div><div><span style="background-color:rgb(255, 255, 255)"><br>

</span></div><div><span style="background-color:rgb(255, 255, 255)"><a href="http://i53.tinypic.com/2r6l5k6.png" target="_blank">http://i53.tinypic.com/2r6l5k6.png</a></span></div><div><span style="background-color:rgb(255, 255, 255)"><br>

</span></div><div><span style="background-color:rgb(255, 255, 255)">The only &quot;problem&quot; is that now I cannot show the &quot;grid&quot; (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)</span></div>

<div><span style="color:rgb(68, 68, 68);font-family:Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif;font-size:13px;line-height:17px;background-color:rgb(255, 255, 255)"><br>
</span></div><div>If this is useful for someone else, I attach the &quot;corrected version&quot; of my code (which produces the image above).</div><div><br></div><div>Thanks again!</div><div><br></div><div>=========================corrected version========================</div>

<div><font color="#444444" face="Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif"><span style="line-height:17px"><br></span></font></div><div><font color="#444444" face="Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif"><span style="line-height:17px"><div>

import numpy as np</div><div>import matplotlib.pyplot as plt</div><div>import matplotlib.backends.backend_agg</div><div><br></div><div>#array between 0 and 360 deg</div><div>#CAVEAT: it seems that is needed an array from -180 to 180, so is just a</div>

<div>#shift in the coordinates</div><div>RA = np.random.random(10000)*360-180</div><div>#array between -45 and 90 degrees</div><div>DEC= np.random.random(10000)*135-45</div><div><br></div><div>fig = plt.Figure((10, 5))</div>

<div>ax = fig.add_subplot(111,projection=&#39;mollweide&#39;)</div><div><br></div><div>ax.set_xlabel(&#39;RA&#39;)</div><div>ax.set_ylabel(&#39;DEC&#39;)</div><div><br></div><div>ax.set_xticklabels(np.arange(30,331,30))</div>

<div><br></div><div>#The ax.grid is not working though =/</div><div>ax.grid(color=&#39;r&#39;, linestyle=&#39;-&#39;, linewidth=2)</div><div><br></div><div>hist,xedges,yedges = np.histogram2d(DEC,RA,bins=[60,40],range=[[-90,90],[-180,180]])</div>

<div><br></div><div>X,Y = np.meshgrid(np.radians(yedges),np.radians(xedges))</div><div><br></div><div>image = ax.pcolormesh(X,Y,hist)</div><div><br></div><div>cb = fig.colorbar(image, orientation=&#39;horizontal&#39;)</div>

<div>canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig)</div><div>fig.canvas.print_figure(&quot;image4.png&quot;)</div></span></font></div><div><font color="#444444" face="Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif"><span style="line-height:17px"><br>

</span></font></div><div><font color="#444444" face="Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif"><span style="line-height:17px"><br></span></font></div>
<div><br><div class="gmail_quote">2011/9/13 Matt Davis <span dir="ltr">&lt;<a href="mailto:mrdavis@stsci.edu" target="_blank">mrdavis@stsci.edu</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div style="word-wrap:break-word"><div>Hi Eduardo,</div><div><br></div><div>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&#39;s what you see in the resulting plot. Imshow *does not* account for the projection.</div>

<div><br></div><div>I think you would prefer to use pcolor so that your data is mapped to the projection. See: <a href="http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.pcolor" target="_blank">http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.pcolor</a></div>

<div><br></div><div>Best,</div><div><br></div><div>Matt Davis</div><br><div><div><div></div><div><div>On Sep 13, 2011, at 9:38 AM, Eduardo Bañados Torres wrote:</div><br></div></div><blockquote type="cite"><div>
<div></div><div><div dir="ltr"><span></span>Hi all,<br><br>I posted this question in Stackoverflow (<a href="http://stackoverflow.com/questions/7355497/curious-bad-behavior-creating-all-sky-projections-with-matplotlib" target="_blank">http://stackoverflow.com/questions/7355497/curious-bad-behavior-creating-all-sky-projections-with-matplotlib</a>) but I haven&#39;t get any answer so far, so I hope some of you can help me :-)<br>


<br>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:<br>


<br>image1.png -&gt; <a href="http://i56.tinypic.com/24mu96s.png" target="_blank">http://i56.tinypic.com/24mu96s.png</a><br><br>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. <br>


<br>But I would expect this image:<br><br>image2.png -&gt; <a href="http://oi53.tinypic.com/2yl1nch.jpg" target="_blank">http://oi53.tinypic.com/2yl1nch.jpg</a><br>A plot ranging between 0 to360 and -45 to 90 as it was defined<br>

<br>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&#39;t notice now  or misunderstanding something in the code or if there is a curious bug in 
matplotlib??<br><br><br>############the self-contained example################<br><pre><code><span>import</span><span> numpy </span><span>as</span><span> np<br>
</span><span>import</span><span> matplotlib</span><span>.</span><span>pyplot </span><span>as</span><span> plt<br></span><span>import</span><span> matplotlib</span><span>.</span><span>backends</span><span>.</span><span>backend_agg<br>


</span><span>from</span><span> math </span><span>import</span><span> pi<br><br></span><span>#array between 0 and 360 deg</span><span><br>RA </span><span>=</span><span> np</span><span>.</span><span>random</span><span>.</span><span>random</span><span>(</span><span>10000</span><span>)*</span><span>360</span><span><br>


</span><span>#array between -45 and 90 degrees. By construction!</span><span><br>DEC</span><span>=</span><span> np</span><span>.</span><span>random</span><span>.</span><span>random</span><span>(</span><span>10000</span><span>)*</span><span>135</span><span>-</span><span>45</span><span><br>


<br>fig </span><span>=</span><span> plt</span><span>.</span><span>Figure</span><span>((</span><span>10</span><span>,</span><span> 5</span><span></span><span>))</span><span><br>
ax </span><span>=</span><span> fig</span><span>.</span><span>add_subplot</span><span>(</span><span>111</span><span>,</span><span>projection</span><span>=</span><span>&#39;mollweide&#39;</span><span>)</span><span><br>
ax</span><span>.</span><span>grid</span><span>(</span><span>True</span><span>)</span><span><br>ax</span><span>.</span><span>set_xlabel</span><span>(</span><span>&#39;RA&#39;</span><span>)</span><span><br>
ax</span><span>.</span><span>set_ylabel</span><span>(</span><span>&#39;DEC&#39;</span><span>)</span><span><br><br>ax</span><span>.</span><span>set_xticklabels</span><span>(</span><span>np</span><span>.</span><span>arange</span><span>(</span><span>30</span><span>,</span><span>331</span><span>,</span><span>30</span><span>))</span><span><br>


hist</span><span>,</span><span>xedges</span><span>,</span><span>yedges </span><span>=</span><span> np</span><span>.</span><span>histogram2d</span><span>(</span><span>DEC</span><span>,</span><span>RA</span><span>,</span><span>bins</span><span>=[</span><span>90</span><span>,</span><span>180</span><span>],</span><span>range</span><span>=[[-</span><span>90</span><span>,</span><span>90</span><span>],[</span><span>0</span><span>,</span><span>360</span><span>]])</span><span><br>


</span><span>#TO RECOVER THE EXPECTED BEHAVIOUR (image2.png), I HAVE TO CHANGE -90 FOR -80 IN THE PREVIOUS LINE:</span><span><br></span><span>#hist,xedges,yedges = np.histogram2d(DEC,RA,bins=[90,180],range=[[-80,90],[0,360]])</span><span><br>


</span><span>#I DO NOT WHY!</span><span><br><br>extent </span><span>=</span><span> </span><span>(-</span><span>pi</span><span>,</span><span>pi</span><span>,-</span><span>pi</span><span>/</span><span>2.</span><span>,</span><span>pi</span><span>/</span><span>2.</span><span>)</span><span><br>


image </span><span>=</span><span> ax</span><span>.</span><span>imshow</span><span>(</span><span>hist</span><span>,</span><span>extent</span><span>=</span><span>extent</span><span>,</span><span>clip_on</span><span>=</span><span>False</span><span>,</span><span>aspect</span><span>=</span><span>0.5</span><span>,</span><span>origin</span><span>=</span><span>&#39;lower&#39;</span><span>)</span><span><br>


<br>cb </span><span>=</span><span> fig</span><span>.</span><span>colorbar</span><span>(</span><span>image</span><span>,</span><span> orientation</span><span>=</span><span>&#39;horizontal&#39;</span><span>)</span><span><br>


canvas </span><span>=</span><span> matplotlib</span><span>.</span><span>backends</span><span>.</span><span>backend_agg</span><span>.</span><span>FigureCanvasAgg</span><span>(</span><span>fig</span><span>)</span><span><br>


<br>fig</span><span>.</span><span>canvas</span><span>.</span><span>print_figure</span><span>(</span><span>&quot;image1.png&quot;</span><span>)</span><span><br>
</span></code></pre>######################################################<br><br>Thanks,<br><br><br><br>-- <br>Eduardo Bañados<br><br>
</div></div></div>
_______________________________________________<br>AstroPy mailing list<br><a href="mailto:AstroPy@scipy.org" target="_blank">AstroPy@scipy.org</a><br><a href="http://mail.scipy.org/mailman/listinfo/astropy" target="_blank">http://mail.scipy.org/mailman/listinfo/astropy</a><br>

</blockquote></div><br></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>Eduardo Bañados <br><br>
</div></div>
</blockquote></div><br></div></div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>Eduardo<br>
</div></div>