繁体   English   中英

基于使用 pandas 的另一列的值在 map 上绘制数据框的选定列的问题

[英]Issue plotting selected columns of a data-frame on a map based on the values of another column using pandas

我是 python 编程的新手。 我有一个超过 1000 行的 csv 文件。 我想根据 map 上的列值(峰值电流)来 plot 列(纬度和经度)。 每当我使用 pandas DataFrame 加载数据时,我通常都会收到错误消息。 但是,当我输入纬度和经度的值(前 10 个值)时,一切似乎都很好。 尽管对于所选数据点,我无法根据数据帧中的峰值电流 plot 经纬度。 我想知道该怎么做。 非常感谢任何帮助。

这是我的代码

from mpl_toolkits.basemap import Basemap
import matplotlib.lines as mlines
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import pandas as pd

lon_0=2

#Loading data

#df = pd.read_csv('file.csv')
#lons = df['longitude']
#lat = df['latitude']

lons = [30.59294,30.593789,30.586066,30.648234,30.646268,30.633628,30.66251,29.531931,30.656409,30.708208]
lat = [-26.67026,-26.651906,-26.650199,-27.012214,-27.002663,-26.982482,-26.739707,-26.795177,-26.995863,-26.766377]

#coordinates
#llcrnrlat,llcrnrlon,urcrnrlat,urcrnrlon
# are the lat/lon values of the lower left and upper right corners of the map.
# lat_ts is the latitude of true scale.

m = Basemap(projection='geos',lon_0=lon_0,resolution='l',\
        llcrnrlat=-36,urcrnrlat=-21,llcrnrlon=15,urcrnrlon=34,)

m.drawcoastlines()
m.drawcountries()
m.drawstates()



# draw parallels and meridians.
parallels = np.arange(-90.,91.,5.)
# Label the meridians and parallels
m.drawparallels(parallels,labels=[False,True,True,False])
# Draw Meridians and Labels
meridians = np.arange(-180.,181.,10.)
m.drawmeridians(meridians,labels=[True,False,False,True])
m.drawmapboundary(fill_color='white')



x,y = m(lons, lat)
plt.plot(x, y, '*',markersize=5)

plt.show()

如果我取消注释下面的行以使用 pandas 加载数据,我通常会收到错误消息

#df = pd.read_csv('file.csv')
#lons = df['longitude']
#lat = df['latitude']

这是我要加载的数据的一部分

    timestamp                   latitude    longitude   peakcurrent icheight    numbersensors   majoraxis   minoraxis   bearing
 2016-01-07T19:00:00.206710100          -26.67026   30.59294    -38161         0            6     0.1          0.1      10.9
 2016-01-07T19:00:00.262988806          -26.651906  30.593789   -49949         0            6     0.1          0.1      13.9
 2016-01-07T19:00:00.387655020          -26.650199  30.586066   27485          0            6     0.51         0.24     10.9
 2016-01-07T19:00:02.242107391          -27.012214  30.648234   -39139         0            6     0.79         0.2      20.9
 2016-01-07T19:00:02.353171110          -27.002663  30.646268   53449          0            6     0.17         0.11     13.3
 2016-01-07T19:00:02.410721779          -26.982482  30.633628   -31396         0            6     0.95         0.64     33.9
 2016-01-07T19:00:02.446598530          -26.739707  30.66251    53774          0            6     0.66         0.12     13.5
 2016-01-07T19:00:02.452036619          -26.795177  29.531931   -36773         0            6     2            0.37     39
 2016-01-07T19:00:02.524655104          -26.995863  30.656409   33640          0            6     1.5          0.37     33.7
 2016-01-07T19:00:02.617701054          -26.766377  30.708208   -74489         0            7     1.23         0.37     21


您需要适当地获取longlat的值。 这是已更正的相关代码。

# ...
lons = df.longitude.values  # needs .values here
lats = df.latitude.values   # same as above

x, y = m(lons, lats)
plt.plot(x, y, '*', markersize=60, zorder=20, alpha=0.3)

plt.show()

output plot 将(使用示例数据)类似于此:

非洲

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM