Seaborn 调色板:如何选择以哪个部分为中心(例如,绘图的哪一端是红色,哪一端是蓝色)? [英] Seaborn color palette: how to choose which part to center on (e.g. which end of the plot is red and which end is blue)?

查看:60
本文介绍了Seaborn 调色板:如何选择以哪个部分为中心(例如,绘图的哪一端是红色,哪一端是蓝色)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 Pandas 数据框的猫图,其中包含一年中所有小时的数值.它有 3 列:Hour、Weekday 和 Value.我是这样绘制的:

I want a catplot of a Pandas dataframe that contains a numerical value for all hours of a year. It has 3 columns: Hour, Weekday, and Value. I plot it like this:

cat_weekdayhour = plt.figure(figsize=(12,12))
cat_weekdayhour = sns.set_context("paper")
cat_weekdayhour = sns.set(style="darkgrid", font_scale=.6)

weekdayhour.shape
cat_weekdayhour = sns.catplot(x="Weekday", y="Value", hue="Hour", kind="swarm", palette="coolwarm", data=dataframe)

这给了我下面的猫图,但我不喜欢一天的凌晨(比如凌晨 0-4 点)很蓝,然后最后几个小时(晚上 8 点到 11 点)是红色的.相反,我想将红色颜色与白天时间居中,然后将整个夜间时间设为蓝色em>.这能做到吗?谢谢.

This gives me the following catplot, but I don't like how the early hours of a day (like 0-4 AM) are very blue and then the last hours (8-11 PM) are red. Instead, I want to center the RED color to the DAY hours and then make all the night hours blue. Can this be done? Thank you.

推荐答案

通过将coolwarm"与其备受推崇的调色板相结合,创建您自己的调色板.我有一个旧版本的 seaborn 所以我将使用 swarmplot 来说明

Create your own pallete by combining "coolwarm" with its reveresed pallette. I have an older version of seaborn so I'll use swarmplot to illustrate

import seaborn as sns
import numpy as np
import pandas as pd

n = 1000
np.random.seed(123)
df = pd.DataFrame({'Weekday': ['Friday']*n,
                   'Hour': np.random.randint(0,24,n),
                   'Value': np.random.randint(40,150,n)})

冷暖调色板

sns.swarmplot(x="Weekday", y="Value", hue="Hour", palette="coolwarm", data=df)

# 24 hours so split evenly between the two
mypal = sns.color_palette("coolwarm", 12) + sns.color_palette("coolwarm_r", 12)
sns.swarmplot(x="Weekday", y="Value", hue="Hour", palette=mypal, data=df)

这篇关于Seaborn 调色板:如何选择以哪个部分为中心(例如,绘图的哪一端是红色,哪一端是蓝色)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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