不使用Matplotlib绘制线条/轨迹

时间:2017-08-09 19:39:57

标签: python csv matplotlib matplotlib-basemap

我正在尝试使用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)

结果:

enter image description here

我正在使用颜色正确绘制标记,但线条不存在。

1 个答案:

答案 0 :(得分:1)

要启用折线图,必须为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()

结果情节:

enter image description here