python - 基于年龄相似度的散点图渐变色

标签 python pandas matplotlib

我有来自不同社区的人,我想根据他们的距离使用不同颜色的散点图直观地绘制:如果他们靠近其他点,他们应该采用相似的颜色。

我知道对于散点图我应该使用以下内容

ax1 = df.plot.scatter(x='Year',
                       y='Community')

但我不知道如何设置类似的条件。 例如:

Community            Year
com1                 2006
com2                 2012
com3                 2006
com4                 2013
com5                 1996
com6                 2008
...

我应该让 com1 和 com3 颜色相同,com6 颜色相似,而 com2 和 com4 应该颜色相似。

我认为这与渐变有关。 你能给我一些关于如何在 python 中执行此操作的信息吗?

最佳答案

让我们将 Year 作为颜色传递:

ax1 = df.plot.scatter(x='Year',y='Community', c=df['Year'], cmap='hot')

输出:

enter image description here

关于python - 基于年龄相似度的散点图渐变色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64345357/

相关文章:

Python 版本依赖问题 - pip uninstall

python - 通过 RPy 对 lme4.lmer 输出调用方差分析

python - 建议向单元格批量添加值的正确方法取决于其他单元格

python - 从数据框列值末尾删除字符

python - 如何在matplotlib中调整暂停动画的坐标范围?

python - NameError : name 'defaultParams' is not defined while running the . exe 使用 Pyinstaller 转换

python - 在python中等待xml文件写入完成

python - Raspbian-Windows openCV 视频流之间的套接字

python - 在python中搜索包含通配符的字符串

python - Pandas:如何将 MultiIndex DataFrame 与单个索引 DataFrame 连接起来,以及自定义排序