为什么我的图不显示其坐标点?

时间:2018-06-25 06:32:27

标签: python python-3.x matplotlib matplotlib-basemap

我正在尝试使用Matplotlib的Basemap类来显示一堆美国城市,它们的位置和点随城市人口的大小而变化。

当我尝试绘制信息时,m.plot和m.scatter都无法显示我想要显示的点。

这是代码

import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import json
import random

#===========GETTING DATA FROM THE JSON FILE========#
path = r"cities_data.json"
json_file = open(path,"r", encoding="utf-8") #open file from path to read
cities_info = json.load(json_file) #load the json file
json_file.close() #close

print(cities_info[1]["city"])
print(len(cities_info))

#take the city names out of every single dictionary in the json
names = []
for x in range(len(cities_info)):
    temp = cities_info[x]["city"]
    names.append(temp)

#take the latitudes
lats = []
for x in range(len(cities_info)):
    temp = cities_info[x]["latitude"]
    lats.append(temp)

#take the longitudes
longs = []
for x in range(len(cities_info)):
    temp = cities_info[x]["longitude"]
    longs.append(temp)

#get the population of each city
pops = []
for x in range(len(cities_info)):
    temp = cities_info[x]["population"]
    pops.append(int(temp))

print(pops)

#normalization of the data
nPops = [(x - np.min(pops))/(np.max(pops) - np.min(pops)) for x in pops]

#store a basemap object with the mercator projection
m =     Basemap(projection="mill",llcrnrlat=25,llcrnrlon=-130,urcrnrlat=50,urcrnrlon=-60)

#draw the coast lines of the countries
m.drawcoastlines()

#color the continents red
m.fillcontinents()

#draw the countries
m.drawcountries()

#draw states
m.drawstates()

#==============FUNCTIONS===============#

#create a function that randomly picks a color (as HEX decimal)
def random_color(number_of_colors):
    color = ["#" + ''.join([random.choice('0123456789ABCDEF') for j in range(6)])
         for i in range(number_of_colors)]
    print(color)
    return color


#plotting the position of NYC
NYClat, NYClon = 40.7, -74.006

#sending coordinates and setting them as x y coordinates
xpt, ypt = m(NYClon, NYClat)

#sending the city coordinates into the m object as x, y (CC = City coordinates)
#xCC, yCC = m([longs],[lats])

#seems like I need to plot using a for loop -- will use a function to do this
def plot_points(lats, longs):
    xCC = []
    yCC = []

    for i in range(len(lats)):
        temp1, temp2 = m(lats[x],longs[x])
        xCC.append(temp1)
        yCC.append(temp2)

    return xCC,yCC

#plotting the coordinates
points = plot_points(lats,longs)
xpts = points[0]
ypts = points[1]

#plotting that coordinate
m.scatter(xpts,ypts,c="b")
plt.title("Tutorial World Map")
plt.show()

对于我使用的NYC坐标(其唯一的示例进行了硬编码),它显示得很好,但是由于某些原因,我无法显示坐标数组。

为什么我不能显示坐标数组?

0 个答案:

没有答案