0

I need to understand a way to make the map fit the scatter plot points

I've looked for solutions and have found matplotlib and apect ratio of geographical-data plots to be somewhat helpful, but I still do not understand longitude and latitude and how to work with them in matplotlib. I've tried changing the aspect to 'equal' and it only makes a small difference in the points. My theory at the moment is I need to figure out how to find the correct aspect ratio by doing some math to make the image fit the points, but I'm not sure what to calculate.

enter image description here

is what it currently looks like, and you can see the general shape of the US from the eastern points. How would I go about this problem? Any references or sources where I can learn more about this would be awesome.

coordinatelist = [list of longitude and latitude coordinates]
#order of coordinates => [longitude min, longitude max, latitude min, latitude max]
coords = [-160.2, -65.5, 14.5, 65.7]
img= plt.imread(path)

width = 636 #width of image in pixels
height = 485 #height of image in pixels 

fig, axes = plt.subplots(figsize = (10,9))
axes.scatter(coordinatelist.longitude, coordinatelist.latitude, zorder=1, alpha= 0.4, c='r', s=8)
axes.set_xlim(coords[0],coords[1])
axes.set_ylim(coords[2],coords[3])
axes.imshow(img, zorder=0, extent = coords, aspect = 'auto')

plt.show()

I would like the points to match the background image, and if possible an explanation of how to reproduce the results so I can gain a deeper understanding. Thank you!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Entroyp
  • 61
  • 1
  • 9
  • You need to know over which range of longitudes and latitudes your images extends. If it doesn't match, it means that either the extent is wrong, or the coordinates are not the ones you'd expect. – ImportanceOfBeingErnest Sep 30 '19 at 00:11
  • Thank you for commenting! I've started to manually change the extent in ```axes.imshow``` and it seems to be working. Do you perhaps know how the origin is set up in imshow? When I extend more to the right, the whole image seems to shift. – Entroyp Sep 30 '19 at 00:36
  • Or perhaps an equation I'm not familiar with to find the right extent? – Entroyp Sep 30 '19 at 00:50
  • If the third value in extent is smaller than the fourth, the axis extents from bottom to top, hence the origin is the lower left corner. [This tutorial](https://matplotlib.org/3.1.1/tutorials/intermediate/imshow_extent.html) would explain it. – ImportanceOfBeingErnest Sep 30 '19 at 01:06
  • Thank you very much! I will look into this but I am on the right track. I appreciate this! – Entroyp Sep 30 '19 at 01:11

1 Answers1

1

Solved:

Manually changing the extent of the map was an unnecessary and inconsistent solution. Openstreemap was exporting the incorrect dimensions which I was not aware of at the time. I found that simply creating your coordinate box on OSM did not actually export what was specified unless you also set custom dimensions.

Upon paying attention to the details closer, the correct size map was exported and the map fit the scatter plots.

Entroyp
  • 61
  • 1
  • 9