0

This is my first time using Matplotlib. I have a series of latitude and longitude co-ordinates in two lists, and I want to represent these in a meaningful manner. I would not like to use Basemap for several reasons.

lat = ['35.905333', '35.896389', '35.901281', '35.860491', '35.807607', '35.832267', '35.882414', '35.983794', '35.974463', '35.930951']
long = ['14.471970', '14.477780', '14.518173', '14.572245', '14.535320', '14.455894', '14.373217', '14.336096', '14.351006', '14.401137']

I am trying to represent these in a meaningful manner using Matplotlib.

import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
plt.scatter(lat, long)
plt.show()

However my figure is as follows:

enter image description here

I am unable to set the axis in order to obtain a meaningful representation of these coordinates. How can this be done? What am I doing wrong?

I am looking for something like this:

import numpy as np
import matplotlib.pyplot as plt

N = 50
x = np.random.rand(N)
y = np.random.rand(N)

plt.scatter(x, y)
plt.show()

I get the expected outcome.

enter image description here

I have also tried to plot on a cartesian coordinate system.


EDIT:

As per the comment below:

import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
plt.scatter(lat, long)
plt.axis('square')
plt.show()

enter image description here

rrz0
  • 2,182
  • 5
  • 30
  • 65
  • Try doing `plt.axis('equal')` or `plt.axis('sqaure')` to get the desired effect. [Source](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.axis.html) – Rick M. Nov 09 '18 at 10:51
  • Thanks for your comment, however this does not set the axis correctly. I also tried setting specific x and y ranges, but this did not work. – rrz0 Nov 09 '18 at 10:55
  • 2
    Your numbers are strings. Convert them to floats – DavidG Nov 09 '18 at 10:59
  • 1
    You need to change the type of the lists `lat` and `long` (may be also the name for long). Try, `lat = np.array(['35.905333', '35.896389', '35.901281', '35.860491', '35.807607', '35.832267', '35.882414', '35.983794', '35.974463', '35.930951'], dtype=float)` – Rick M. Nov 09 '18 at 10:59
  • @DavidG, cannot believe I overlooked this. Read data from file and missed the conversion. It worked. – rrz0 Nov 09 '18 at 11:03
  • Use Basemap. Geographical coordinates ***must*** be projected on the plot plane. – gboffi Nov 09 '18 at 11:52

1 Answers1

2

As mentioned in the comment, change the type to float:

import numpy as np
import matplotlib.pyplot as plt

lat = np.array(['35.905333', '35.896389', '35.901281', '35.860491', '35.807607', 
'35.832267', '35.882414', '35.983794', '35.974463', '35.930951'], dtype=float)
long = np.array(['14.471970', '14.477780', '14.518173', '14.572245', '14.535320', 
'14.455894', '14.373217', '14.336096', '14.351006', '14.401137'], dtype=float)

fig, ax = plt.subplots(figsize=(10, 6))
ax.scatter(lat, long)
# ax.axis('equal')
plt.show()

enter image description here

Rick M.
  • 3,045
  • 1
  • 21
  • 39