我正在使用住房数据集用于我自己的学习目的,我希望能够将我的图叠加在地图上,以便更好地了解“热点”。
我的代码如下:
housing = pd.read_csv('https://raw.githubusercontent.com/ageron/handson-ml/master/datasets/housing/housing.csv')
plt.figure()
housing.plot(x='longitude', y='latitude', kind='scatter', alpha=0.4,
s= housing['population']/100, label='population', figsize=(10,7),
c= 'median_house_value', cmap=plt.get_cmap('jet'), colorbar=True, zorder=5)
plt.legend()
plt.show()
我保存为“California.png”的图像
这是我尝试过的:
img=imread('California.png')
plt.figure()
plt.imshow(img,zorder=0)
housing.plot(x='longitude', y='latitude', kind='scatter', alpha=0.4,
s= housing['population']/100, label='population', figsize=(10,7),
c= 'median_house_value', cmap=plt.get_cmap('jet'), colorbar=True, zorder=5)
plt.legend()
plt.show()
但这只是给了我两个情节。我试过切换索引无济于事。
有没有简单的方法来实现这一点?谢谢。
编辑:使用@nbeuchat 下面的代码:
plt.figure(figsize=(10,7))
img=imread('California.png')
plt.imshow(img,zorder=0)
ax = plt.gca()
housing.plot(x='longitude', y='latitude', kind='scatter', alpha=0.4,
s= housing['population']/100, label='population', ax=ax,
c= 'median_house_value', cmap=plt.get_cmap('jet'), colorbar=True,
zorder=5)
plt.legend()
plt.show()
我得到以下情节:
您正在使用数据框绘图函数创建一个新图窗。您应该传递要在其上绘制第二个图的轴。一种方法是使用gca
获取当前轴。
以下应该工作(虽然未测试):
plt.figure(figsize=(10,7))
img=imread('California.png')
plt.imshow(img,zorder=0,extent=[housing['longitude'].min(),housing['longitude'].max(),housing['latitude'].min(),housing['latitude'].max()])
ax = plt.gca()
housing.plot(x='longitude', y='latitude', kind='scatter', alpha=0.4,
s= housing['population']/100, label='population', ax=ax,
c= 'median_house_value', cmap=plt.get_cmap('jet'), colorbar=True,
zorder=5)
plt.legend()
plt.show()
编辑:使用具有经度和纬度数据的最小值和最大值的extent
参数imshow
将正确缩放图像。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句