Reputation: 18978
This is my first question on Stack Overflow, so my apologies if I've overlooked anything. I'm making a plot of cruise positions, and I am getting a double-parallel happening at 60 degrees North, with one being a straight line (on a stereographic projection).
Does anybody know what I'm doing to cause this?
My plotting script (referencing external data):
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
import sys
import csv
dataFile = sys.argv[1]
dataStream = open(dataFile, 'rb')
dataReader = csv.reader(dataStream, delimiter=',')
numRows = sys.argv[2]
dataLat = []
dataLon = []
dataReader.next()
for row in dataReader:
dataLon.append(float(row[5]))
dataLat.append(float(row[6]))
m = Basemap(width=450000,height=150000,
resolution='f',projection='stere',\
lat_ts=65.4,lat_0=60.4,lon_0=1.91)
m.drawcoastlines(linewidth=0.2)
m.fillcontinents(color='white', lake_color='aqua')
x, y = m(dataLat,dataLon)
m.scatter(x,y,.5,marker='.',color='k')
m.drawparallels(np.arange(0.,81,1.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(-180.,181.,5.), labels=[0,0,0,1], fontsize=10)
m.drawmapboundary(fill_color='aqua')
plt.title("Cruise Track")
plt.show()
Example:
One other small question: what are the units of the width / height of the plot? It does not appear to be in the documentation, and I can't find it mentioned on any tutorials, etc.
Upvotes: 2
Views: 1725
Reputation: 5522
Seems like a bug due to trying to fix another one.
I don't use basemap, but the first step to try and solve problems is to reduce them as much as possible:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
scale = 1
m = Basemap(width=450000*scale,height=150000,
resolution='i',projection='stere',
lat_ts=65.4,lat_0=60.4,lon_0=1.91)
parallels = [60]
ret = m.drawparallels(parallels)
plt.show()
You can see in the docs that drawparallels
returns a dict, and we can recover the actual Line2D (the parallel) and its data points:
l = ret[60][0][0]
x, y = l.get_data()
If we have a look at the points, it doesn't look bad unless we link them, and then we can see the annoying double parallel!
plt.plot(x, y, '+')
plt.show()
plt.plot(x, y)
plt.show()
Let's now try running the small example through the debugger (python -m pdb small_example.py
).
If you step inside the call to drawparallels, you'll soon reach this code. It doesn't look too good, so let's skip the highlighted lines (using jump
, if you've never used the Python debugger you should read a bit about it).
And voilà, this is the result:
Taking into account where is the problem, it's easy to guess that changing the width or height of the map (in meters, by the way) could get rid of the problem. Indeed, setting scale>=1.0485
works. Other combinations of width and height seem to work fine too.
Upvotes: 2