2 回答
TA贡献2016条经验 获得超9个赞
问题:如何在底图的顶部绘制不同密度的散点图?
数据:我有印度各个城市的经纬度以及各个年份的污染水平。
方法:最初,我已将分配的数据结构分配给变量“df”。将纬度分配给“y”,将经度分配给“x”。由于纬度和经度也有一个变量'y'和'x',在其中,我使用'df['x'].values'来单独获取数值。考虑到印度的几何位置,我绘制了地图。下一组是将这些纬度和经度纳入与地图相同的变量中。这是至关重要的,没有它,我无法观察散点图。以颜色作为第三个变量,我绘制了散点图,污染水平具有不同的密度。
代码:
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
# make up some data for scatter plot
df = pd.read_csv('dust.csv')
x = df[['y']].values
y = df[['x']].values
fig = plt.gcf()
fig.set_size_inches(8, 6.5)
m = Basemap(projection='mill',
llcrnrlat = 6.5,#llcrncrlat=lower left corner latitude
llcrnrlon = 66,
urcrnrlat = 36,
urcrnrlon = 98,
resolution = 'l')
#m.bluemarble(scale=0.2) # full scale will be overkill
m.drawcoastlines(linewidth=1)# add coastlines
m.drawcountries(linewidth=1)
# transform coordinates
#plt.subplot(221)
x,y=m(x,y)
plt.scatter(x, y,s=df.iloc[1:,2], alpha = 0.5, cmap='viridis')
plt.colorbar()
plt.show()
TA贡献1776条经验 获得超12个赞
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
# make up some data for scatter plot
df = pd.read_csv('dust.csv')
x = df[['y']].values
y = df[['x']].values
fig = plt.gcf()
fig.set_size_inches(8, 6.5)
m = Basemap(projection='mill',
llcrnrlat = 6.5,#llcrncrlat=lower left corner latitude
llcrnrlon = 66,
urcrnrlat = 36,
urcrnrlon = 98,
resolution = 'l')
#m.bluemarble(scale=0.2) # full scale will be overkill
m.drawcoastlines(linewidth=1)# add coastlines
m.drawcountries(linewidth=1)
# transform coordinates
#plt.subplot(221)
x,y=m(x,y)
plt.scatter(x, y,s=df.iloc[1:,2], alpha = 0.5, cmap='viridis')
plt.colorbar()
plt.show()
添加回答
举报