Pandas 散点图类型错误

Pandas scatter plot TypeError

我正在尝试使用 geopandas 在 shapefile 中绘制点,但我一直遇到

TypeError: You must first set_array for mappable

每当我 运行 下面的代码。当我删除 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')