'Matplotlib not plotting at all correctly, not seen anyone else have this issue

So, I'm trying to code with matplotlib, so that it plots coordinates of the majority of cities in the USA on a graph. As should be evident by the fact I'm asking this question, it isn't working. The code is just plotting all the points on a single diagonal line, shown below, and both axises are completely out of order (you can see it clearly on the y-axis). Below is both an image of the result, and the matplotlib code I'm using: Image of multiple points on a single diagonal line with no order to either axises

Code:

def demand(places, distfact):
  demandList = []
  fig = plt.figure()
  print(len(places))
  for origin in places:
    for destination in places:
      if origin != destination:
        dist = haversine(float(origin.lat), float(origin.lon), float(destination.lat),
                     float(destination.lon))
        result = (int(origin.population) * int(destination.population)) / (dist * distfact)
        line1 = [origin.name, destination.name, result,
             [origin.lat, origin.lon], [destination.lat, destination.lon]]
        line2 = [destination.name, origin.name, result,
             [destination.lat, destination.lon], [origin.lat, origin.lon]]
        if line2 not in demandList and result >= 75000000 and dist >= 30.0:
          demandList.append(line1)
  demandList.sort(key=lambda row: (row[2]), reverse=True)
  for i in range(0, 20):
    print(demandList[i][0], "->", demandList[i][1], ":", "{:,}".format(demandList[i][2]))
  print("\n")
  print(len(demandList) - 30, "routes")
  print("\n")
  for i in range(0,10):
    ind = len(demandList) - i - 1
    print(demandList[ind][0], "->", demandList[ind][1], ":", "{:,}".format(demandList[ind][2]))
  for i in range(len(demandList)):  
    xpoints = np.array([demandList[i][3][0], demandList[i][4][0]])
    ypoints = np.array([demandList[i][3][1], demandList[i][4][1]])
    plt.plot(xpoints, ypoints, "o")

"places" is a list of objects. Each object contains a townID, name, population, latitude and longtitude. distfact is simply a number, in this example it's set to 5.



Solution 1:[1]

Just a guess as I can't see what's in your variables: try casting the values from your x_points and y_points variables to whatever makes sense in your case be it float or int. It seems to me that they are seen as strings by pyplot.

edit: Not a guess anymore, you're missing casts to float, when populating line1 with latitudes and longitudes. You do do it in your haversine computations but not three lines below.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1