2017-04-22 48 views
1

數據(火車)取自Kaggle TitanicMatplotlib:將組結果的顏色更改爲

我有以下情節:

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

我想改變線條的顏色。問題是我不能簡單地在這裏使用顏色參數。那麼我如何解決它們呢? plot

回答

1

您不能直接使用顏色參數,因爲直方圖被劃分爲多個軸。

解決方法可能是爲腳本設置色循環器,即指定哪些顏色應該隨後由繪製任何東西的任何函數逐一使用。這可以通過使用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

相關問題