1
数据(火车)取自Kaggle Titanic。Matplotlib:将组结果的颜色更改为
我有以下情节:
train.groupby(["Survived", "Sex"])['Age'].plot(kind='hist', legend = True, histtype='step', bins =15)
我想改变线条的颜色。问题是我不能简单地在这里使用颜色参数。那么我如何解决它们呢?
数据(火车)取自Kaggle Titanic。Matplotlib:将组结果的颜色更改为
我有以下情节:
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()