Python 在地图上画不同大小的圆圈
我有以下已排序的数据帧(数字完全是随机的): 每个位置都有一个纬度和经度 我想用圆圈在地图上标出这些位置。圆的半径需要与Python 在地图上画不同大小的圆圈,python,pandas,matplotlib,matplotlib-basemap,Python,Pandas,Matplotlib,Matplotlib Basemap,我有以下已排序的数据帧(数字完全是随机的): 每个位置都有一个纬度和经度 我想用圆圈在地图上标出这些位置。圆的半径需要与总数中的数量相对应。换句话说,位置1需要有最大的圆,位置2需要有较小的圆,等等 另外,我想有一个颜色的过渡。最大的圆圈是红色的,下一个是橙色的,下一个是黄色的,等等 最后,我想在每个圆旁边做一个注释 我设法在地图上画出了蓝点,但我不知道如何画出相应大小和颜色的圆圈 这是我目前的代码: m = Basemap(resolution='i', projection='merc',
总数中的数量相对应。换句话说,位置1需要有最大的圆,位置2需要有较小的圆,等等
另外,我想有一个颜色的过渡。最大的圆圈是红色的,下一个是橙色的,下一个是黄色的,等等
最后,我想在每个圆旁边做一个注释
我设法在地图上画出了蓝点,但我不知道如何画出相应大小和颜色的圆圈
这是我目前的代码:
m = Basemap(resolution='i', projection='merc', llcrnrlat=49.0, urcrnrlat=52.0, llcrnrlon=1., urcrnrlon=8.0, lat_ts=51.0)
m.drawcountries()
m.drawcoastlines()
m.fillcontinents()
for row_index, row in df.iterrows():
x, y = db.getLocation(row_index)
lat, lon = m(y, x)
m.plot(lat, lon, 'b.', alpha=0.5)
#This draws blue dots.
plt.title('Top 10 Locations')
plt.show()
- matplotlib
scatter
函数具有s
和c
参数,可用于绘制不同大小和颜色的点
指定kind='scatter'
时,PandasDataFrame.plot
方法调用matplotlibscatter
函数。它还将额外的参数传递给对scatter
的调用,这样您就可以使用
df.plot(kind='scatter', x='lon', y='lat', s=df['Total']*50, c=df['Total'], cmap=cmap)
来描绘你的观点
- 可以通过调用
plt.annotate
来完成
- 从红色到橙色再到黄色。。。给紫罗兰
gist\u rainbow\r
是反向的颜色映射,它使红色
对应于最大值
比如说,
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'Total': [20,15,13,1],
'lat': [40,0,-30,50],
'lon': [40,50,60,70], },
index=['Location {}'.format(i) for i in range(1,5)])
cmap = plt.get_cmap('gist_rainbow_r')
df.plot(kind='scatter', x='lon', y='lat', s=df['Total']*50, c=df['Total'], cmap=cmap)
for idx, row in df.iterrows():
x, y = row[['lon','lat']]
plt.annotate(
str(idx),
xy = (x, y), xytext = (-20, 20),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
plt.show()
屈服
不要为每个点调用df.plot
或plt.scatter
一次。随着圆点数量的增加,这将变得非常缓慢。相反,在数据框中收集必要的数据(经度和纬度),以便通过调用df.plot
:
longitudes, latitudes = [], []
for row_index, row in df.iterrows():
x, y = db.getLocation(row_index)
lat, lon = m(y, x)
longitudes.append(lon)
latitudes.append(lat)
plt.annotate(
str(row_index),
xy = (x, y), xytext = (-20, 20),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
df['lon'] = longitudes
df['lat'] = latitudes
cmap = plt.get_cmap('gist_rainbow_r')
ax = plt.gca()
df.plot(kind='scatter', x='lon', y='lat', s=df['Total']*50, c=df['Total'],
cmap=cmap, ax=ax)
非常好和详细的回答!我还有一个问题。我无法访问df.iterrows()
之外的纬度和经度。我必须从数据库中获取这些数据(正如我的示例代码所示:x,y=db.getLocation(row\u index)
。我可以在循环的每次迭代中使用plt.scatter
来获得相同的结果吗?在这种情况下,我如何安排颜色来使用cmap?不要在循环的每次迭代中调用plt.scatter
。这样做数百个点可能会使脚本非常慢。而是收集lon
和lat
s在列表或数据框中,因此点可以通过调用df.plot
或plt.scatter
来绘制。我编辑了上面的帖子来展示这样一种方式。我几乎成功了。现在的问题是,它绘制了两个图。一次是带注释的基本图,一次是数据框。哎呀,我的错误。df.plot默认情况下,code>创建一个新轴,但如果将轴
对象传递给它,则会使用该轴。因此ax=plt.gca()
(获取当前轴)和df.plot(…,ax=ax)
是修复方法(我已在上面进行了更正)
longitudes, latitudes = [], []
for row_index, row in df.iterrows():
x, y = db.getLocation(row_index)
lat, lon = m(y, x)
longitudes.append(lon)
latitudes.append(lat)
plt.annotate(
str(row_index),
xy = (x, y), xytext = (-20, 20),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
df['lon'] = longitudes
df['lat'] = latitudes
cmap = plt.get_cmap('gist_rainbow_r')
ax = plt.gca()
df.plot(kind='scatter', x='lon', y='lat', s=df['Total']*50, c=df['Total'],
cmap=cmap, ax=ax)