1

I am trying to plot lines and markers from a CSV file on a map using matplotlib.

Data:

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 Code:

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)

Result:

enter image description here

I am getting the markers to correctly plot with color but, the lines are not there.

2
  • 2
    Just a reminder, your code is incomplete (what is m?) and incorrectly formatted (no indent after the for loop). Besides, your data doesn't seem to match your plot (for example, your lat in data is only between 15 and 20; yet your plot looks like on Atlantic with lat at least up to 40). So your question might be difficult for others to troubleshoot and help. Commented Aug 9, 2017 at 21:52
  • Don't expect to get an answer when you don't supply complete code. Unless you are very lucky. Usually, it will take much more effort than necessary on the part of people who care to answer. Commented Aug 10, 2017 at 6:36

1 Answer 1

1

To enable line plot, the plot() function must be supplied with arrays of multi points (x, y), not single values as you did. Here are a working code and resulting output plot (based on your supplied data).

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()

Resulting plot:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.