Python Matplotlib:更改group by结果的颜色

Python Matplotlib:更改group by结果的颜色,python,pandas,matplotlib,Python,Pandas,Matplotlib,数据(列车)取自 我有以下情节: train.groupby(["Survived", "Sex"])['Age'].plot(kind='hist', legend = True, histtype='step', bins =15) 我想改变线条的颜色。问题是我不能在这里简单地使用颜色参数。那么我该如何称呼他们呢? 无法直接使用“颜色”参数,因为直方图被划分为多个轴 一种解决方法是为脚本设置颜色循环器,即指定绘制任何内容的任何函数随后应逐个使用哪些颜色。这将使用pyplot的rcParam

数据(列车)取自

我有以下情节:

train.groupby(["Survived", "Sex"])['Age'].plot(kind='hist', legend = True, histtype='step', bins =15)
我想改变线条的颜色。问题是我不能在这里简单地使用颜色参数。那么我该如何称呼他们呢?

无法直接使用“颜色”参数,因为直方图被划分为多个轴

一种解决方法是为脚本设置颜色循环器,即指定绘制任何内容的任何函数随后应逐个使用哪些颜色。这将使用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()