gpt4 book ai didi

python - 未使用 Matplotlib 绘制线条/轨迹

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:43 28 4
gpt4 key购买 nike

我正在尝试使用 matplotlib 在 map 上绘制 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

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

最佳答案

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

关于python - 未使用 Matplotlib 绘制线条/轨迹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45599322/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com