如何使用 cartopy 散布范围颜色的绘图值?
How to scatter plot values in a range color with cartopy?
我有这个df
:
STATION LONGITUDE LATITUDE TMAXPERC
0 000130 -80.45750 -3.81333 9.034495
1 000132 -80.45722 -3.50833 7.291477
2 000134 -80.23722 -3.57611 15.760175
3 000135 -80.32194 -3.44056 5.256434
4 000136 -80.66083 -3.94889 12.301515
.. ... ... ... ...
366 158323 -69.99972 -17.55917 57.318854
367 158325 -69.94889 -17.66083 87.762365
368 158326 -69.74556 -17.18639 40.719109
369 158327 -69.81306 -17.23722 86.950173
370 158328 -69.77944 -17.52500 53.413032
[371 rows x 4 columns]
我正在使用这段代码绘制经纬度散点图。
import cartopy.crs as ccrs
import pandas as pd
import cartopy
%matplotlib inline
import numpy as np
import cartopy.io.img_tiles as cimgt
import cartopy.io.shapereader as shpreader
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
data = pd.ExcelFile("path/filename.xlsx")
df =pd.read_excel(data,'Hoja1',dtype={'STATION': str})
df=df[['STATION','LONGITUDE','LATITUDE','TMAXPERC']]
reader = shpreader.Reader('C:/Ubuntu/Python/Shapefile/Dz/direcciones.shp')
counties = list(reader.geometries())
COUNTIES = cfeature.ShapelyFeature(counties, ccrs.PlateCarree())
plt.figure(figsize=(15,15))
stamen_terrain = cimgt.Stamen('terrain-background')
m10 = plt.axes(projection=stamen_terrain.crs)
plt.scatter(
x=df["LONGITUDE"],
y=df["LATITUDE"],
color="red",
s=4,
alpha=1,
transform=ccrs.PlateCarree()
)
# (x0, x1, y0, y1)
m10.set_extent([-85, -65, 2, -20], ccrs.PlateCarree())
# add map, zoom level
m10.add_image(stamen_terrain, 8)
m10.add_feature(COUNTIES, facecolor='none', edgecolor='black')
m10.legend()
m10.gridlines()
plt.show()
这是结果:
但我想用 df['TMAXPERC']
制作散点图。我想根据相应纬度和经度中的 TMAXPERC 值,使用 scalecolor 在地图中绘制点。我怎样才能做到这一点?提前致谢。
您需要正确指定一些选项:
plt.scatter(
x=df["LONGITUDE"],
y=df["LATITUDE"],
c=df["TMAXPERC"], cmap='viridis', #this is the changes
s=4,
alpha=1,
transform=ccrs.PlateCarree()
)
我有这个df
:
STATION LONGITUDE LATITUDE TMAXPERC
0 000130 -80.45750 -3.81333 9.034495
1 000132 -80.45722 -3.50833 7.291477
2 000134 -80.23722 -3.57611 15.760175
3 000135 -80.32194 -3.44056 5.256434
4 000136 -80.66083 -3.94889 12.301515
.. ... ... ... ...
366 158323 -69.99972 -17.55917 57.318854
367 158325 -69.94889 -17.66083 87.762365
368 158326 -69.74556 -17.18639 40.719109
369 158327 -69.81306 -17.23722 86.950173
370 158328 -69.77944 -17.52500 53.413032
[371 rows x 4 columns]
我正在使用这段代码绘制经纬度散点图。
import cartopy.crs as ccrs
import pandas as pd
import cartopy
%matplotlib inline
import numpy as np
import cartopy.io.img_tiles as cimgt
import cartopy.io.shapereader as shpreader
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
data = pd.ExcelFile("path/filename.xlsx")
df =pd.read_excel(data,'Hoja1',dtype={'STATION': str})
df=df[['STATION','LONGITUDE','LATITUDE','TMAXPERC']]
reader = shpreader.Reader('C:/Ubuntu/Python/Shapefile/Dz/direcciones.shp')
counties = list(reader.geometries())
COUNTIES = cfeature.ShapelyFeature(counties, ccrs.PlateCarree())
plt.figure(figsize=(15,15))
stamen_terrain = cimgt.Stamen('terrain-background')
m10 = plt.axes(projection=stamen_terrain.crs)
plt.scatter(
x=df["LONGITUDE"],
y=df["LATITUDE"],
color="red",
s=4,
alpha=1,
transform=ccrs.PlateCarree()
)
# (x0, x1, y0, y1)
m10.set_extent([-85, -65, 2, -20], ccrs.PlateCarree())
# add map, zoom level
m10.add_image(stamen_terrain, 8)
m10.add_feature(COUNTIES, facecolor='none', edgecolor='black')
m10.legend()
m10.gridlines()
plt.show()
这是结果:
但我想用 df['TMAXPERC']
制作散点图。我想根据相应纬度和经度中的 TMAXPERC 值,使用 scalecolor 在地图中绘制点。我怎样才能做到这一点?提前致谢。
您需要正确指定一些选项:
plt.scatter(
x=df["LONGITUDE"],
y=df["LATITUDE"],
c=df["TMAXPERC"], cmap='viridis', #this is the changes
s=4,
alpha=1,
transform=ccrs.PlateCarree()
)