简体   繁体   English

Seaborn调色板具有Pandas groupby和.plot功能

[英]Seaborn color palette with Pandas groupby and .plot function

I am having a very frustrating problem when trying to plot some data with a seaborn color palette. 尝试使用深浅的调色板绘制一些数据时,我遇到了一个非常令人沮丧的问题。 My workflow is to perform a groupby operation on my dataframe, and plot each group as its own curve using code like this: 我的工作流程是对数据框执行groupby操作,并使用如下代码将每个组绘制为自己的曲线:

import seaborn as sns; sns.set_style('whitegrid')
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(16, 6))

inner = d.groupby(['dr_min', 'dr_max'])
n = len(inner)
cmap = sns.color_palette("Blues", n_colors=n)
inner.plot(x='limit_l', y='lccdf', ax=ax1, color=cmap, legend=False)
inner.plot(x='limit_r', y='rcdf', ax=ax2, color=cmap, legend=False)

I expect to see a curve for each one of my groupby parameters with clear shades from the color map, as seen here: 我希望我的groupby参数中的每个参数都能从颜色图中看到清晰的阴影曲线,如下所示: 在此处输入图片说明

Instead, my curves look below, with no color grade at all. 取而代之的是,我的曲线朝下,完全没有颜色等级。 Can anyone help me understanding why this is happening? 谁能帮助我了解为什么会这样?

在此处输入图片说明

Possibly you want to iterate over the groupby object to chose the color for each individual line. 可能您想遍历groupby对象以为每行选择颜色。

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

a = np.tile(np.arange(0,10),5)
b = np.linspace(0,1,len(a))
c = np.repeat(list("ABCDE"), 10)
df = pd.DataFrame({"x":a, "y":b, "c":c})

fig, ax = plt.subplots()
cmap = sns.color_palette("Blues", n_colors=5)

inner = df.groupby(["c"])

for i, (n, gr) in enumerate(inner):
    gr.plot(x="x", y="y", ax=ax, color=cmap[i])

plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM