Polar stereographic plot - python

Is it possible to plot contours over a polar stereographic map with the latest version of cartopy?
I'd like to see an example of how this is done as I'm struggling to work it out myself!

The stereographic projection is causing a couple of headaches and is probably the projection which has raised the most issues for cartopy's polygon transformations code.
The following example show how one should produce a polar stereographic plot with cartopy.
Please note: even with this code, it is possible to tweak the sample data resolution and find that the plot takes ~30 minutes to actually render (that is a bug which we will need to sort sooner rather than later).
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from cartopy.examples.waves import sample_data
ax = plt.axes(projection=ccrs.NorthPolarStereo())
x, y, z = sample_data((100, 200))
cs = ax.contourf(x, y, z, 50,
transform=ccrs.PlateCarree(),
cmap='gist_ncar')
ax.coastlines()
# without the set_global, currently, the plot is tiny because the limits
# are being erroneously being set (opened issue for that)
ax.set_global()
plt.show()
Hopefully that will show you how one should make a polar stereographic contour plot in cartopy. If you having problems with your data have a look at the open issues tagged "Geometry transforms" and see if you are getting something similar, if not, go ahead and open an issue and we can look into it.
Note: This answer is relating to cartopy v0.5.x (i.e. just before a v0.5 release), and many of the bugs mentioned here should hopefully be squashed in future releases.
Hope that helps,

Related

How to create a raw data plot of a directional variable in Matplotlib

I have a dataset of wind directions and I am looking to create raw data plots as seen in the example below. I should mention that I know about rose diagrams and how to create them in Python. This is another way of looking at the data and is commonly brought up in texts about directional statistics.
The plot is similar to a polar histogram, in that raw data is collected into continuous bins. But there are stacked individual dots for each data point instead of a bar.
I have found a solution on the mathematica stackexchange, but I have no experience with that language: circular plot and circular histogram. The book I took the above image from also gives the R code for it, but I only know Python.
Below is the minimal code I have until now.
directions = np.random.randint(low=0, high=90, size=50)
# Setup plot
ax = plt.subplot(111, polar=True)
ax.set_theta_zero_location("N")
ax.grid(False)
plt.show()
As you can see I have not made much progress. Again, I can find code for polar bar plots and histograms, but not for this type of plot in Python. Any hints in the right directions are welcome.

Issue with plotting shapefiles on an orthographic projection in Basemap

I have been trying to plot Exclusive Economic Zone (EEZ) shapefiles on an orthographic projection from the basemap package. However, the shapefile file has EEZs from around the world, and so when I try to plot the shapefiles there are always some that are not visible in the projection at that particular angle. This results in the shapes being smeared out, which is not quite the effect that I am going for. Ultimately I wish to only plot select shapefiles, but then this same issue will likely pop up so for now I'd be happy to solve this more basic case where I try to plot all of them.
Here in the code I try a simple case where I plot the shapefiles with the readshapefile command from basemap. I have also tried plotting the various shapes as polygons (figured that would give me more flexibility in changing the appearances of the individual shapefiles) but then I could not get the polygons to appear on the map in the right spot and I would see similar smearing behavior (so likely the issue has the same or a similar root cause).
I have attached the code from the simple case below. If I run this, I get the projection to appear as a I want, but with the smearing of the shapefiles. The shapefiles can be found at http://www.marineregions.org/downloads.php#unioneezcountry where I use version 2 of Marine and land zones: the union of world country boundaries and EEZ's.
#Here is the figure
fig=plt.figure(figsize=(20,12))
ax=fig.add_subplot(111)
#create the map projection
Map=Basemap(projection='ortho',lon_0=0,lat_0=0,resolution='l')
Map.drawcoastlines(zorder=10)
Map.drawcountries(zorder=10)
Map.drawmapboundary()
#Reading in the shapefile and plotting it
Map.readshapefile('~/EEZ_Boundaries/EEZ_land_v2_201410','countries')
Here is a link to the image I get when I run the code
Ok, so after more time of trying to get this to work, I have pretty much given up with Basemap and made a (long overdue) switch to cartopy. In that case, the problem does solved by Cartopy already, so the code that creates the figure I was trying to get is:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cpf
from cartopy.io.shapereader import Reader
#Set the projection
projection=ccrs.Orthographic(central_longitude=0,central_latitude=0)
fig=plt.figure(figsize=(20,12))
axMap=fig.add_subplot(1,1,1,projection=projection)
#resolution of the coastlines
resolution='10m'
axMap.coastlines(resolution=resolution,edgecolor='black',zorder=10)
#Add the shapefiles
shape_feature = cpf.ShapelyFeature(Reader(direc_shp+file_shp).geometries(),
ccrs.PlateCarree(), edgecolor='black')
axMap.add_feature(shape_feature,zorder=1)

project numpy array to aitoff projection

I have an (as yet incomplete) image of the whole sky. In order to display it properly it needs to be projected onto an ellipse (specifically an Aitoff projection).
I have tried various versions of this:
plt.subplot(111, projection="aitoff")
plt.imshow(image, vmin=0.004, vmax=0.01, extent=[0,360,-90,90])
plt.show()
...and have tried changing the values in the extent kwarg to radians, as well as using pcolor or pcolormesh instead of imshow.
These have given me: an empty Aitoff plot, various Aitoff plots with all or part of my image sitting inside it, but not filling it, or an Aitoff plot with a small part of my image (one or two pixels by the looks of things) completely filling it.
My whole image sitting within a plot
The unprojected image
I also do not have access to things like Basemap or astroproj as I'm using a machine owned by my university.
Edit: As was pointed out by another user the above example is not Minimal, Complete, and Verifiable. Below is a version which should be:
A=np.random.rand(180,360)
plt.imshow(A)
plt.show()
plt.subplot(111, projection="aitoff")
plt.pcolormesh(A)
plt.show()
I want the entire image generated in the plt.imshow() command to be projected in the Aitoff figure. Instead only a few pixels are. Any ideas?
Thanks!
Using imshow in non-rectilinear projections will mostly fail. But instead pcolormesh may be used.
The aitoff projection ranges from -π to π in horizontal and from -π/2 to π/2 in vertical direction. This is the range of values to use when plotting the pcolormesh plot.
import numpy as np
import matplotlib.pyplot as plt
im = plt.imread("house.jpg")
x = np.linspace(-np.pi,np.pi,im.shape[1])
y = np.linspace(-np.pi/2,np.pi/2,im.shape[0])
X,Y = np.meshgrid(x,y)
plt.subplot(111, projection="aitoff")
plt.pcolormesh(X,Y[::-1],im[:,:,2])
plt.show()

Matplotlib offset errorbar bug workaround?

The bug is documented here:
Matplotlib errorbar not centered on marker
and here:
https://github.com/matplotlib/matplotlib/issues/3400
Basically, the markers are plotted off by 1 pixel all the time.. You can even see this on Matplotlib's own tutorial page if you look closely at the second plot: http://matplotlib.org/1.2.1/examples/pylab_examples/errorbar_demo.html
This is very frustrating as I cannot produce publication-quality plots from matplotlib, and I'm very surprised this has not been fixed.
In any case, I have too much time and code invested into matplotlib to switch to a different package. So my question is how would you go about making a workaround? I suppose one solution is to plot the markers 1 pixel to the left/right from the errorbars. I don't know how to do this. I figured out how to get the display coordinates of my plot points, but how can I make an interactive plot that preserves the 1-pixel offset? I can plot them with 1-pixel offsets, but then you can't zoom or manipulate the plot.
It seems like the Matplotlib team have fixed the issue when calling savefig() using .svg or .pdf, but for .png I've found that you can circumvent this issue by using an odd number for the error line thickness. Using the first example on the Matplotlib tutorial, if we use
plt.errorbar(x, y, yerr=0.4, marker='X', markersize=15)
then the bars are offset like this:
However if we use a line width of 3
plt.errorbar(x, y, yerr=0.4, marker='X', markersize=15, elinewidth=3)
then the bars are centred like this:
This isn't a perfect solution, but it does the job if you don't mind having slightly thicker lines.

How do I let my matplotlib plot go beyond the axes?

I have to translate an image plotting script from matlab to matplotlib/pylab, and I'm trying to achieve the same effect as the matlab image below:
As you can see, the z order of the plots seem to be higher than the z order of the grid, so the markers are not hidden by the axes. However, I can't figure out a way to do the same with my matplotlib image:
I'm wondering if it is possible to get the same display without having to increase the limits of the y axis.
To get the marker to show beyond the axes you can turn the clipping off. This can be done using the keyword argument in the plot command clip_on=False.
For example:
import matplotlib.pyplot as plt
plt.plot(range(5), range(5), 'ro', markersize=20, clip_on=False, zorder=100)
plt.show()
This is a complete example of how to use the zorder kwarg: http://matplotlib.sourceforge.net/examples/pylab_examples/zorder_demo.html
Note that a higher z-order equates to a graph-element being more in the foreground.
For your second question, have a look at the figsize kwarg to instances of the Figure class: http://matplotlib.sourceforge.net/api/figure_api.html?highlight=figsize#matplotlib.figure.Figure
If you run into issues, please post some of your code and we'll be able to give more-detailed recommendations. Best of luck.
If you're plotting the lines one after the other, just change the order of the plotting calls and that would fix the z order.

Categories

Resources