Matplotlib:更改分组结果的颜色 [英] Matplotlib: change the colors of the result of group by

查看:63
本文介绍了Matplotlib:更改分组结果的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据(火车)取自

解决方案

由于直方图分布在多个轴上,因此无法直接使用color参数.

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

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

完整的示例:

 将seaborn.apionly导入为sns将熊猫作为pd导入导入matplotlib.pyplot作为plt从循环仪进口循环仪plt.rc('axes',prop_cycle =(cycler('color',['r','g','b','c']))))泰坦尼克号 = sns.load_dataset("泰坦尼克号")gdf = titanic.groupby(["survived","sex"])['age']ax = gdf.plot(种类='hist',图例= True,histt​​ype ='step',箱= 15)plt.show()

Data (train) is taken from Kaggle Titanic.

I have a following plot:

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

I want to change the colors of the lines. The problem is that I cannot simply use the color argument here. So how do I address them?

解决方案

You cannot directly use the color argument because the histograms are divided over several axes.

A workaround may be to set the color cycler for the script, i.e. specify which colors should subsequently be used one by one by any function that plots anything. This would be done using the rcParams of pyplot.

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

Full working example:

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()

这篇关于Matplotlib:更改分组结果的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆