2

I am using matplotlib basemap to plot a trajectory from data in a pandas dataframe.

Simply plotting lat, lon data as below works fine:

map = Basemap(projection='stere',lon_0=lon,lat_0=lat, llcrnrlon=-65, llcrnrlat=55, urcrnrlon=50 , urcrnrlat=75)

map.drawcountries()
map.bluemarble()

#Place marker at Kangerlussuaq
x,y = map(lon, lat)
map.plot(x, y, 'bo', markersize=10) 
plt.text(x, y, ' Kangerlussuaq', fontsize=12)

#Plot trajectory
map.plot(datacopy.long.tolist(), 
            datacopy.lat.tolist(), 
            'r-', 
            latlon=True)

However, I want parts of the trajectory to be plotted with a different marker depending on the ambient temperature measured at each lat/lon but the following code gives me the error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

if datacopy['Ambient temp']<280:
map.plot(datacopy.long.tolist(), 
        datacopy.lat.tolist(), 
        'r-', 
        latlon=True)
else: 
    map.plot(datacopy.long.tolist(), 
            datacopy.lat.tolist(), 
            'b-', 
            latlon=True)

Is this error due to the data being in a pandas dataframe and is there a way around it?

Thanks

Serenity
  • 35,289
  • 20
  • 120
  • 115
Bethany
  • 35
  • 3

1 Answers1

2

First of all your conditional series may be empty. You have to check it.

To plot conditional series look at this example:

import pandas as pd
import numpy as np
import matplotlib.pylab as plt

# Create a dataframe
df = pd.DataFrame( {'lat':[1,2,3,4,5], 'long':[1,2,3,4,5],  'T': [42, 52, 36, 24, 70]})
print (df)

plt.figure(figsize = (5, 5))

# selection condition
df1 = df[(df['T'] < 50)]
# ~ means not
df2 = df[~(df['T'] < 50)]
print(df2)
print(df1)
# plot
plt.plot(df1.long, df1.lat, 'b+')
ax = plt.gca()
ax.plot(df2.long, df2.lat, 'r*')

plt.show()

enter image description here

Test output:

    T  lat  long
0  42    1     1
1  52    2     2
2  36    3     3
3  24    4     4
4  70    5     5
    T  lat  long
1  52    2     2
4  70    5     5
    T  lat  long
0  42    1     1
2  36    3     3
3  24    4     4

As well you do not need to convert series to lists.

Serenity
  • 35,289
  • 20
  • 120
  • 115
  • Also you may use scatter plot as mentioned here https://stackoverflow.com/questions/43018428/pandas-python-matplotlib-scatter-plot-markers-colour-depending-on-a-value-from-a – Serenity May 16 '18 at 18:45