Python 熊猫散点图类型错误
我试图使用Python 熊猫散点图类型错误,python,python-3.x,pandas,scatter-plot,Python,Python 3.x,Pandas,Scatter Plot,我试图使用geopandas在一个shapefile中绘制点,但我一直遇到这样的问题 TypeError:必须首先为可映射设置_数组 每当我运行下面的代码时。当我删除colormap属性时,此错误消失。但是我想改变我的点的颜色,我认为colormap对此很有帮助 这是我的密码: import matplotlib.pyplot as plt import geopandas shapefile = geopandas.GeoDataFrame.from_file('file.shp') fi
geopandas
在一个shapefile中绘制点,但我一直遇到这样的问题
TypeError:必须首先为可映射设置_数组
每当我运行下面的代码时。当我删除colormap
属性时,此错误消失。但是我想改变我的点的颜色,我认为colormap
对此很有帮助
这是我的密码:
import matplotlib.pyplot as plt
import geopandas
shapefile = geopandas.GeoDataFrame.from_file('file.shp')
fig, ax = plt.subplots(1)
base = shapefile.plot(ax=ax)
df.plot.scatter('Long', 'Lat', c=df['colC'], s=df['colD'], alpha=0.7, ax=base, colormap='viridis')
您可以尝试直接调用
matplotlib
。我没有您的数据集可用于尝试此操作,但请尝试以下操作:
from operator import itemgetter
import geopandas
import matplotlib.pyplot as plt
shapefile = geopandas.GeoDataFrame.from_file('file.shp')
fig, ax = plt.subplots()
shapefile.plot(ax=ax)
x, y, c, s = itemgetter('Long', 'Lat', 'colC', 'colD')(df)
ax.scatter(x, y, c=c, s=s, cmap='viridis')
'colC'
中存储的类型是什么?浮点数…如果将c=df['colC']
更改为c='colC'
,同时保持colormap
参数不变,会发生什么情况?我遇到相同的错误:(很高兴听到这个消息!我刚刚意识到它也可能是您对ax=base
的规范。我的猜测是GeoDataFrame.plot
不会返回ax
对象。最好将相同的Axes
对象显式传递给您感兴趣调用的每个绘图调用。