ImportError:无法从“matplotlib.cbook”导入名称“dedent”

2024-04-26 08:26:58 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,当尝试导入basemap时,我遇到此错误

ImportError: cannot import name 'dedent' from 'matplotlib.cbook'

我试过很多解决问题的方法,但都没办法解决。 我正在使用windows和Anaconda

thisreddit中,它使用“make”命令,但这在windows上对我不起作用。从我的谷歌搜索中,我发现它的原因是一个linux命令

我也试着从不同的地方下载底图,但它并没有真正帮助我

我还读到basemap已经贬值,现在人们使用cartopy,但我在导入cartopy时也遇到了问题

我所要做的就是在美国地图上叠加数据,这给了我很多问题。如果有人能帮助我,我将非常感激

对于那些好奇的人,如果我可以用另一种方式来实现,下面是我尝试实现的basemap代码:

df_city_salary = df[["Location","average_salary"]]
scale=1

map = ccrs(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49,
        projection='lcc',lat_1=32,lat_2=45,lon_0=-95)

# load the shapefile, use the name 'states'
map.readshapefile('st99_d00', name='states', drawbounds=True)

# Get the location of each city and plot it
geolocator = Nominatim()
for (city,count) in df_city_salary:
    loc = geolocator.geocode(city)
    x, y = map(loc.longitude, loc.latitude)
    map.plot(x,y,marker='o',color='Red',markersize=int(math.sqrt(count))*scale)
plt.show()

TL;DR:安装basemap的最简单方法。ELI5为白痴安装底图


Tags: the方法name命令citymapdfwindows
1条回答
网友
1楼 · 发布于 2024-04-26 08:26:58

以下是我为那些好奇的人所做的:

fig = plt.figure(figsize=(12,10))

ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree())

ax.coastlines()
ax.add_feature(cfeature.STATES)

ax.set_extent([-135, -66.5, 20, 55], crs=ccrs.PlateCarree()) ## Important
df_range = len(loc)
color = df['average_salary'].div((df_range*100))
plt.scatter(coordinates.latitude,coordinates.longitude,alpha = 0.6, c= color, cmap="bwr_r")
plt.show

所以我最终使用了cartopy,这需要一些欺骗。最后,我使用了一个reddit线程,一步一步地介绍了如何安装cartopy。它要求我以特定的方式卸载并重新安装cartopy

这就是输出: enter image description here

相关问题 更多 >