Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect tick labels #204

Closed
ajdawson opened this issue Feb 7, 2013 · 13 comments
Closed

Incorrect tick labels #204

ajdawson opened this issue Feb 7, 2013 · 13 comments
Milestone

Comments

@ajdawson
Copy link
Member

ajdawson commented Feb 7, 2013

When putting tick labels on a cartopy.crs.PlateCarree projection the longitude labels are incorrect if a central longitude of 180 is used. To reproduce:

# labels are incorrect
plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.coastlines(zorder=1)
xticks = range(0, 361, 60)
ax.set_xticks(xticks, crs=ccrs.PlateCarree())
yticks = range(-90, 91, 30)
ax.set_yticks(yticks, crs=ccrs.PlateCarree())
ax.set_title('central longitude = 180')

180

(I also just realised 90S and 90N are missing on the latitude axis too!)

If I use 0 as the central longitude then the longitude labels are correct.

# works fine (as long as longitudes in the range [-180, 180])
plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
ax.coastlines(zorder=1)
xticks = range(-180, 181, 60)
ax.set_xticks(xticks, crs=ccrs.PlateCarree())
yticks = range(-90, 91, 30)
ax.set_yticks(yticks, crs=ccrs.PlateCarree())
ax.set_title('central longitude = 0')

0

Perhaps I'm just doing this wrong?

@pelson
Copy link
Member

pelson commented Feb 7, 2013

There was a pull request merged 2 days ago which adds proper support for tick (in some very limited projections). The ticks you're seeing here are simple matplotlib ticks which have no knowledge of cartopy - hence they are plotting in the "native" coordinate system.

Would you be willing to give the functionality added by @pp-mo in #200 a shot?

@ajdawson
Copy link
Member Author

ajdawson commented Feb 7, 2013

OK you mean using the ax.gridlines() method? I did this:

plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.coastlines(zorder=1)
ax.gridlines(draw_labels=True)
ax.set_title('ax.gridlines(draw_labels=True)')

which produces:

gridlines

which is obviously not quite right! I guess I have misunderstood. I thought because GeoAxes has its own set_xticks method that accepts a crs that it would handle things correctly? Anyway, what I would like to be able to do is label the left and bottom axes, and show only ticks, not grid lines. Is that possible?

@pelson
Copy link
Member

pelson commented Feb 7, 2013

which is obviously not quite right!

Agreed! 😄

I thought because GeoAxes has its own set_xticks method that accepts a crs that it would handle things correctly?

We've not yet done anything to the set_xticks family of methods which will mean the CRS is handled appropriately (should it always be in lat long?)

Anyway, what I would like to be able to do is label the left and bottom axes, and show only ticks, not grid lines. Is that possible?

In truth, we've not focused on this so much. Your original approach might actually be easier in the short term - I'd think about defining a Formatter which takes the native coordinates and transforms them into the coordinate system you wanted. As for the +/- 90 not showing up, I'm pretty sure there is nothing special going on and that the clipping is part of the standard (though difficult to track down) matplotlib behaviour. If you want to talk about this over a mailing list, you can open a thread on the matplotlib-devel and prefix the subject with cartopy: to see if there are other who can help out with the clipping problem.

Cheers,

@ajdawson
Copy link
Member Author

ajdawson commented Feb 7, 2013

OK well I combined this with ax.set_xticklabels to get what I wanted in a hacky way. The +/- 90 was weird because it only happens when I used PlateCarree(central_longitude=180) but not when I used PlateCarree()... That is partly why I thought there may be a bug in here somewhere...

@bblay
Copy link
Contributor

bblay commented Mar 27, 2013

The mouse-over positions are also reported incorrectly with central_longitude=180.

@pelson
Copy link
Member

pelson commented Mar 27, 2013

The mouse-over positions are also reported incorrectly with central_longitude=180.

@bblay - I can't reproduce this. Are you doing something like:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.coastlines()
plt.show()

@bblay
Copy link
Contributor

bblay commented Mar 28, 2013

Are you doing something like:

Yep, I cut and paste that into python and when I hover the mouse near the left edge it says -178.

I just noticed it also has the correct coords, with units, in brackets afterwards
so this is probably a feature, not a bug.

@ajdawson
Copy link
Member Author

There are still issues with tick marks/gridlines that need to be addressed, but perhaps this particular issue can be closed now and replaced with more specific issues/feature requests?

@pelson
Copy link
Member

pelson commented Mar 28, 2013

Yep, I cut and paste that into python and when I hover the mouse near the left edge it says -178.

Those coordinates are the coordinates of the projection you are plotting (which in your case are shifted by 180 degrees. The coordinates in brackets are the coordinates in "lons and lats" which are probably what you're expecting. Thanks for following this through @bblay.

There are still issues with tick marks/gridlines that need to be addressed, but perhaps this particular issue can be closed now and replaced with more specific issues/feature requests?

I don't want to loose any bug report. I'm pretty certain the repeated 180/-180 exists in the tick marks, is there anything else that I'm missing? I agree we can go ahead and open a specific issue for this (would you mind doing this?). When you're happy that all the issues are covered by folllow-up issues, please go ahead and close this one.

Cheers @ajdawson & @bblay .

@jediefe
Copy link

jediefe commented Nov 18, 2014

As this is an old but still existent problem in cartopy, I found a workaround that works at least for the PlateCarree() projection. In mpl/gridliner.py [cartopy v0.11.0] I inserted the following code at line 239:

        try:
            tmp_value = value+self.crs.proj4_params.get('lon_0')
            if tmp_value > 180.:
                tmp_value = tmp_value-360.
            str_value = self.xformatter(tmp_value)
        except ValueError:
            str_value = self.xformatter(value)

Maybe this is an approach for a patch, but it doesn't solve the problem of incorrect doubled labeling at 180°E/180°W, if central_longitude for the projection is 180, but 0 for gridlines

@ajdawson
Copy link
Member Author

There is now a formatter object provided with cartopy that attempts to resolve this, albeit in a bit of a work-around fashion. See this example for details: http://scitools.org.uk/cartopy/docs/latest/examples/tick_labels.html (although the image was cropped when the documentation is created, if you run the example yourself you should see the correct labels).

@jediefe
Copy link

jediefe commented Nov 18, 2014

In your example the use of set_xticks is demonstrated, while I want to use an instance of cartopy.mpl.gridliner.Gridliner to produce a graphic like it is shown in the appended example.
dapaclip_pacrain_equitable_threat_score

@ajdawson
Copy link
Member Author

Apologies, I didn't read that correctly. This issue refers specifically to tick marks, so I assumed this was your issue. Feel free to open a new issue for this problem, it probably won't get much attention here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants