为特定列创建具有不同颜色的散点图

Creating a Scatter Plot with different colors for a certain column

我目前正在尝试创建具有某些特征的散点图。 我原来的 'eu' 包含具有经度、纬度、区域等城市统计信息的国家/地区。 我创建了一个数据框,将国家/地区与其包含的城市分组,同时显示经度和纬度。

cityScatter = eu[['country', 'city', 'longitude', 'latitude', 'temperature']] # Create a new dataframe to hold specific items.
newCityScatter = cityScatter.groupby(['country', 'city', 'longitude', 'latitude']).count()['temperature'].to_frame() # Group the countries with their respective cities.
newCityScatter = newCityScatter.drop(['temperature'], axis=1) # Remove temperature now that we have it grouped.
newCityScatter # Result

这是我得到的table。这就是我想要的,因为所有国家/地区都与城市组合在一起。 现在我的问题是创建散点图。 我希望图表中每个城市的 x = 经度和 y = 纬度。但是,城市所在的国家必须在图表上有自己的颜色。

例如,格拉茨、因斯布鲁克和林茨可以涂成蓝色,因为它们位于奥地利。同时,爱丁堡、埃克斯特、格拉斯哥、因弗内斯和斯旺西可能是红色的,因为它们居住在英国。

newCityScatter.plot(kind='scatter',x='longitude',y='latitude')

当我 运行 上面的语句时,我得到“类型错误:没有要绘制的数字数据”。 我想弄清楚如何在这些约束条件下绘制散点图。

免责声明:我不能使用 seaborn。我想弄清楚如何仅使用 matplotlib 来做到这一点。

我认为您不需要 .groupby() 或任何其他预处理。

我想你可以这样做:

cityScatter.plot(kind='scatter', x='longitude', y='latitude', c='country')