Lines/Tracks 不使用 Matplotlib 绘图
Lines/Tracks Not Plotting with Matplotlib
我正在尝试使用 matplotlib.
在地图上绘制 CSV 文件中的线条和标记
数据:
AL99,2017080912,SHIP,0,17.1,-55.6,25,0
AL99,2017080912,SHIP,12,18.1,-57.6,27,0
AL99,2017080912,SHIP,24,19.0,-59.2,29,0
AL99,2017080912,SHIP,36,20.1,-60.2,34,0
AL99,2017080912,SHIP,48,21.5,-61.6,39,0
AL99,2017080912,SHIP,60,23.3,-63.0,47,0
AL99,2017080912,SHIP,72,25.4,-65.2,54,0
AL99,2017080912,SHIP,84,27.9,-68.1,61,0
AL99,2017080912,TABD,0,17.1,-55.7,0,0
AL99,2017080912,TABD,6,17.5,-56.7,0,0
AL99,2017080912,TABD,12,17.8,-57.3,0,0
AL99,2017080912,TABD,18,18.1,-57.9,0,0
AL99,2017080912,TABD,24,18.5,-58.3,0,0
AL99,2017080912,TABD,30,19.0,-58.6,0,0
AL99,2017080912,TABD,36,19.6,-58.8,0,0
Python代码:
tc = np.recfromcsv(csv_file, unpack=True, names=['stormid', 'initdate', 'mems', 'times', 'tclat', 'tclon', 'tcwind', 'tcpres'], dtype=None)
for j in range(len(tc.times)):
lon, lat = tc.tclon[j], tc.tclat[j]
xpt, ypt = m(lon, lat)
lonpt, latpt = m(xpt, ypt, inverse=True)
if tc.mems[j] == 'TABD':
tccolor = '--bo'
elif tc.mems[j] == 'AEMN':
tccolor = '-ro'
else:
tccolor = '-k'
m.plot(xpt, ypt, tccolor)
结果:
我正在让标记正确绘制颜色,但是线条不存在。
要启用线图,必须为 plot()
函数提供多点 (x, y) 数组,而不是像您那样提供单个值。
这是一个工作代码和结果输出图(基于您提供的数据)。
from matplotlib import pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np
fig = plt.gcf()
fig.set_size_inches([8,8])
m = Basemap(projection='cyl', \
llcrnrlat= 10, urcrnrlat= 30, \
llcrnrlon= -70, urcrnrlon= -50, \
resolution='l')
csv_file = "storm_data.csv"
tc = np.recfromcsv(csv_file, unpack=True, names=['stormid', 'initdate', 'mems', 'times', 'tclat', 'tclon', 'tcwind', 'tcpres'], dtype=None)
# for each line segment, (xs, ys) is initialized here
# two of them are done here for demo purposes
xs1 = []
ys1 = []
xs2 = []
ys2 = []
for j in range(len(tc.times)):
lon, lat = tc.tclon[j], tc.tclat[j]
xpt, ypt = m(lon, lat)
lonpt, latpt = m(xpt, ypt, inverse=True)
if tc.mems[j] == 'TABD':
xs1.append(lon)
ys1.append(lat)
elif tc.mems[j] == 'SHIP':
xs2.append(lon)
ys2.append(lat)
# *** elif for other line segmems ***
else:
pass
# plot the collected line segments
m.plot( xs1, ys1, '--bo', xs2, ys2, '--ro' )
# draw coastline
m.drawcoastlines()
plt.show()
结果图:
我正在尝试使用 matplotlib.
数据:
AL99,2017080912,SHIP,0,17.1,-55.6,25,0
AL99,2017080912,SHIP,12,18.1,-57.6,27,0
AL99,2017080912,SHIP,24,19.0,-59.2,29,0
AL99,2017080912,SHIP,36,20.1,-60.2,34,0
AL99,2017080912,SHIP,48,21.5,-61.6,39,0
AL99,2017080912,SHIP,60,23.3,-63.0,47,0
AL99,2017080912,SHIP,72,25.4,-65.2,54,0
AL99,2017080912,SHIP,84,27.9,-68.1,61,0
AL99,2017080912,TABD,0,17.1,-55.7,0,0
AL99,2017080912,TABD,6,17.5,-56.7,0,0
AL99,2017080912,TABD,12,17.8,-57.3,0,0
AL99,2017080912,TABD,18,18.1,-57.9,0,0
AL99,2017080912,TABD,24,18.5,-58.3,0,0
AL99,2017080912,TABD,30,19.0,-58.6,0,0
AL99,2017080912,TABD,36,19.6,-58.8,0,0
Python代码:
tc = np.recfromcsv(csv_file, unpack=True, names=['stormid', 'initdate', 'mems', 'times', 'tclat', 'tclon', 'tcwind', 'tcpres'], dtype=None)
for j in range(len(tc.times)):
lon, lat = tc.tclon[j], tc.tclat[j]
xpt, ypt = m(lon, lat)
lonpt, latpt = m(xpt, ypt, inverse=True)
if tc.mems[j] == 'TABD':
tccolor = '--bo'
elif tc.mems[j] == 'AEMN':
tccolor = '-ro'
else:
tccolor = '-k'
m.plot(xpt, ypt, tccolor)
结果:
我正在让标记正确绘制颜色,但是线条不存在。
要启用线图,必须为 plot()
函数提供多点 (x, y) 数组,而不是像您那样提供单个值。
这是一个工作代码和结果输出图(基于您提供的数据)。
from matplotlib import pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np
fig = plt.gcf()
fig.set_size_inches([8,8])
m = Basemap(projection='cyl', \
llcrnrlat= 10, urcrnrlat= 30, \
llcrnrlon= -70, urcrnrlon= -50, \
resolution='l')
csv_file = "storm_data.csv"
tc = np.recfromcsv(csv_file, unpack=True, names=['stormid', 'initdate', 'mems', 'times', 'tclat', 'tclon', 'tcwind', 'tcpres'], dtype=None)
# for each line segment, (xs, ys) is initialized here
# two of them are done here for demo purposes
xs1 = []
ys1 = []
xs2 = []
ys2 = []
for j in range(len(tc.times)):
lon, lat = tc.tclon[j], tc.tclat[j]
xpt, ypt = m(lon, lat)
lonpt, latpt = m(xpt, ypt, inverse=True)
if tc.mems[j] == 'TABD':
xs1.append(lon)
ys1.append(lat)
elif tc.mems[j] == 'SHIP':
xs2.append(lon)
ys2.append(lat)
# *** elif for other line segmems ***
else:
pass
# plot the collected line segments
m.plot( xs1, ys1, '--bo', xs2, ys2, '--ro' )
# draw coastline
m.drawcoastlines()
plt.show()
结果图: