簡體   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