python - 为什么我的绘图不显示其坐标点?
问题描述
我正在尝试使用 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 坐标(它是唯一一个硬编码的示例),它显示得很好,但由于某种原因,我无法显示坐标数组。
为什么我不能显示坐标数组?
解决方案
推荐阅读
- git - 从 Pycharm 删除后如何恢复已删除的分支
- r - 从refund R包中的拟合pfr对象中提取系数函数
- angular - 类型'employee []上不存在属性'emp' - Angular 6 ng serve --prod
- apache-spark - 了解 Spark 如何将输入文件转换为工作节点
- lambda - 使用 Lambda 函数来使用供应商提供的所有对象
- java - 在这里了解多态性
- webpack - 如何在 webpack 中合理使用 process.env 变量
- saml - 在 Node 中为离线应用程序实施 SAML 身份提供程序?
- python - python Popen 不捕获程序的标准输出。不确定为什么
- ruby - “gem install therubyracer”失败并显示错误消息“libv8 需要安装 python 2 才能构建”