如何绘制一个 pd.Series 并使用来自其他数据源的每个点的颜色?

问题描述 投票:0回答:1

我有一个 pd.Series 叫做 data 我想绘制,而我有另一个系列,其指数与 data 叫做 colors. 我想策划 data对于情节中的每一个点,我想根据系列中的相应值给它上色。colors (使用一些颜色图)。是否可以在 matplotlibseaborn?

python pandas matplotlib seaborn
1个回答
1
投票

你可以用以下方法来做 matplotlib 像。

import matplotlib.pyplot as plt

data = pd.Series(range(5))
colors = pd.Series(['r','g','b','b','r'])

plt.scatter(data.index, data, c=colors)
plt.show()

enter image description here

或者如果系列的颜色更像组号(不是颜色),你可以用... seaborn 喜欢

import seaborn as sns

data = pd.Series(range(5))
colors = pd.Series([0, 1, 2, 2, 0])

sns.scatterplot(data.index, data, hue=colors)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.