gpt4 book ai didi

python - 更改group by结果的颜色

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:50 24 4
gpt4 key购买 nike

数据(火车)取自Kaggle Titanic .

我有以下情节:

train.groupby(["Survived", "Sex"])['Age'].plot(kind='hist', legend = True, histtype='step', bins =15)

我想改变线条的颜色。问题是我不能在这里简单地使用颜色参数。那么我该如何解决这些问题呢? plot

最佳答案

您不能直接使用颜色参数,因为直方图分为多个轴。

一种解决方法可能是为脚本设置颜色循环器,即指定随后应由绘制任何内容的任何函数一一使用哪些颜色。这可以使用 pyplot 的 rcParams 来完成。

plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b','c'])))

完整的工作示例:

import seaborn.apionly as sns
import pandas as pd
import matplotlib.pyplot as plt
from cycler import cycler

plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b','c'])))

titanic = sns.load_dataset("titanic")

gdf = titanic.groupby(["survived", "sex"])['age']
ax = gdf.plot(kind='hist', legend = True, histtype='step', bins =15)

plt.show()

enter image description here

关于python - 更改group by结果的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43565499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com