我的代碼的目標是使用賓夕法尼亞州收費公路上出口的緯度和經度來制作粗略的路線圖,并在每個出口之間畫一條線。
每次回圈時,我都使用 for 回圈在地圖上繪制一條線。如果我對緯度和經度進行硬編碼,則此方法有效,但只要我插入變數,就不會繪制任何內容。由于坐標是有序的,我只是在每次回圈獲取下一個坐標時增加索引。我已經在回圈內列印了變數并驗證了它們是否具有所需的值。我嘗試將值按有序對放置,但繪圖函式不喜歡我使用 nparrays。我不確定我是否缺少一些簡單的東西,但我很感激任何意見。
import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs
from datetime import datetime, timedelta
# Open the file for highway metadata to read csv data
highway_metadata = open('milestone3data.csv', 'r')
metafile = csv.reader(highway_metadata, delimiter = ',')
# Create empty lists with highway data
highway_loc = []
highway_name = []
highway_lat = []
highway_lon = []
highway_dist = []
# Loop to transfer the csv file's data into the lists
for i in metafile:
highway_loc.append(i[0])
highway_name.append(i[1])
highway_lat.append(float(i[2]))
highway_lon.append(float(i[3]))
highway_dist.append(i[4])
def road_map():
enhighway_lat = enumerate(highway_lat)
enhighway_lon = enumerate(highway_lon)
orthographic = ccrs.Orthographic()
platecarree = ccrs.PlateCarree()
proj = ccrs.Orthographic(central_longitude = -75, central_latitude = 41)
ax = plt.axes(projection=proj)
# Set up the background
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.STATES)
ax.set_extent((-85,-70,36,45),crs=ccrs.PlateCarree())
for i,j in enhighway_lat:
for k,l in enhighway_lon:
if i or k <= 30:
plt.plot([highway_lon[k], highway_lon[k 1]], [highway_lat[i], highway_lat[i 1]], color='black', linewidth=1, marker='o', markersize=3, transform=ccrs.PlateCarree())
plt.savefig('cartopytest7.png')
plt.show
road_map()
[This is my most recent output from the program][1]
[1]: https://i.stack.imgur.com/lgFrN.png
CSV 檔案內容:(英里標記、出口名稱、緯度、經度、距收費公路起點的英里數)
2,Gateway (Ohio Connection),40.90419167,-80.47158333,1.43
10,New Castle,40.83018056,-80.34196111,10.7
13,Beaver Valley,40.8143,-80.307925,12.87
28,Cranberry,40.67983889,-80.09537778,28.47
30,Warrendale,40.65533889,-80.06116667,31
39,Butler Valley,40.60913611,-79.91924444,39.1
48,Allegheny Valley,40.542025,-79.81022222,47.73
57,Pittsburgh,40.43808889,-79.74956944,56.44
67,Irwin,40.31342778,-79.65476111,67.22
75,New Stanton,40.22173333,-79.59573333,75.39
91,Donegal,40.10915,-79.35231944,90.69
110,Somerset,40.02033056,-79.05208056,109.91
146,Bedford,40.05013889,-78.48615,145.5
161,Breezewood,39.98721667,-78.24472778,161.5
180,Fort Littleton,40.05010556,-77.93954444,179.44
189,Willow Hill,40.09674167,-77.78441389,188.59
201,Blue Mountain,40.15755278,-77.58403333,201.29
226,Carlisle,40.22814722,-77.14782222,226.54
236,Gettysburg Pike,40.19569444,-76.95665556,236.22
242,Harrisburg West Shore,40.21216667,-76.85765278,241.87
247,Harrisburg East,40.21501111,-76.78060278,247.38
266,Lebanon-Lancaster,40.22974444,-76.43095,266.45
286,Reading,40.21805,-76.05189167,286.09
298,Morgantown,40.15990278,-75.88311667,298.33
312,Downingtown,40.06838611,-75.66450278,311.93
320,SR29,40.07641667,-75.52881944,319.33
326,Valley Forge,40.09296667,-75.39591111,326.62
333,Norristown,40.11101111,-75.27921389,333.28
339,Fort Washington,40.13231944,-75.17092222,338.36
340,Virginia Dr,40.13854444,-75.16268611,339.8
343,Willow Grove,40.16166111,-75.11271111,342.91
351,Bensalem,40.13200278,-74.96229444,351.49
352,Street Rd,40.13150833,-74.96445,351.89
353,Neshaminy Falls,40.12916667,-74.94150278,352.67
uj5u.com熱心網友回復:
好的,根據上面的討論,請參閱下面的解決方案。
筆記:
- 我正在使用 pandas DataFames 輕松處理該
.csv檔案。該names欄位是列名。 - 我根本沒有使用正交投影。
一次遍歷一個出口的高速公路出口串列;在每個索引處,我都在提取當前和下一個出口的資料——我確信有一種更“pythonic”的方法可以做到這一點,但這至少是可讀的。編輯:回圈中的最終索引是 length-1- 更新:感謝@SimonWillerton,我洗掉了回圈。
import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs
import pandas as pd
def road_map():
# Open the file for highway metadata to read csv data
highway_metadata = pd.read_csv('milestone3data.csv', names=["loc", "name", "lat", "lon", "dist"])
proj = ccrs.PlateCarree(central_longitude = -75)
ax = plt.axes(projection=proj)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.STATES)
ax.set_extent((-85,-70,36,45),crs=ccrs.PlateCarree())
plt.plot(highway_metadata['lon'], highway_metadata['lat'], \
color='black', linewidth=1, marker='o', markersize=3, transform=ccrs.PlateCarree())
plt.savefig('cartopytest7.png')
if __name__ == '__main__':
road_map()
這將產生以下影像:

而且,根據維基百科的賓夕法尼亞收費公路的這張圖片(來源= 
uj5u.com熱心網友回復:
看起來你正試圖用你的plt.plot()陳述做一些相當復雜的事情。你有經度串列和緯度串列;這就是在 matplotlib 中的點之間繪制所需的全部內容,不需要enumerate或回圈串列。以下行應該可以解決問題。
plt.plot(highway_lon, highway_lat, color='black', linewidth=1, marker='o', markersize=3, transform=ccrs.PlateCarree())
這是洗掉了一些不必要位的代碼。
import csv
import numpy as np
import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs
# Open the file for highway metadata to read csv data
highway_metadata = open('milestone3data.csv', 'r')
metafile = csv.reader(highway_metadata, delimiter = ',')
# Create empty lists with highway data
highway_loc = []
highway_name = []
highway_lat = []
highway_lon = []
highway_dist = []
# Loop to transfer the csv file's data into the lists
for i in metafile:
highway_loc.append(i[0])
highway_name.append(i[1])
highway_lat.append(float(i[2]))
highway_lon.append(float(i[3]))
highway_dist.append(i[4])
def road_map():
fig = plt.figure(figsize=(10, 10))
proj = ccrs.Orthographic(central_longitude = -75,
central_latitude = 41)
ax = plt.axes(projection=proj)
ax.set_extent((-85,-70,36,45),crs=ccrs.PlateCarree())
# Set up the background
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.STATES)
plt.plot(highway_lon, highway_lat,
color='black', linewidth=1, marker='o', markersize=3,
transform=ccrs.PlateCarree())
plt.savefig('cartopytest7.png')
plt.show
road_map()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/462322.html
標籤:Python CSV matplotlib 卡托普
上一篇:python和sql查詢中的問題
下一篇:如果提示為空,如何停止js腳本
