如何在海运图例中组合色调和样式组? [英] How can I combine hue and style groups in a Seaborn legend?

查看:20
本文介绍了如何在海运图例中组合色调和样式组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为使用hue按";subscale";分组的纵向数据和使用style按";项目";分组的纵向数据绘制海运线条图。

以下是我的代码(希望没有数据也可以理解):

ax = sns.lineplot(data = df, x = 'Week', y = 'Value', style = 'Item', hue = 'Subscale', palette = 'colorblind', markers = True)
plt.legend(bbox_to_anchor = (1.03, 1.02), fontsize = 10)

这给我带来了这个情节:

我想要的是合并子图例,以便它只显示";项目";的图例,但项目根据";subscale";着色,类似于:

我创建这个失败了,所以如果你们中的任何人能帮忙,我将不胜感激!谢谢:)

推荐答案

如果我理解正确,某一类型的所有项目都有相同的分量表。因此,您已经拥有(或可以创建)将项目类型映射到相应小数位数的字典。

Seborn为图例创建以下标签:

  • 字幕的‘subscale’
  • 每个分量表
  • 第二个副标题的‘Item’
  • 每一项

每个标签对应于标识标记外观的句柄(&q;)。

以下代码:

  • 查找'Item'的索引以能够拆分标签和句柄的数组
  • 提取"Subscale"的颜色
  • 将这些颜色应用于项目标记
  • 仅使用图例的项目
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

# first create some random data similar to the description
np.random.seed(123)
items = ['01', '04', '05', '06', '07', '10', '11', '13']
N = len(items)
subscale_dict = {'01': 'A', '04': 'C', '05': 'C', '06': 'C', '07': 'A', '10': 'B', '11': 'B', '13': 'A'}

df = pd.DataFrame({'Week': np.tile(np.arange(7), N),
                   'Value': np.random.rand(7 * N),
                   'Item': np.repeat(items, 7)})
df['Subscale'] = df['Item'].apply(lambda i: subscale_dict[i])
df['Subscale'] = pd.Categorical(df['Subscale'])  # creates a fixed order

# create the line plot as before
ax = sns.lineplot(data=df, x='Week', y='Value', style='Item', hue='Subscale', palette='colorblind', markers=True)

# create a dictionary mapping the subscales to their color
handles, labels = ax.get_legend_handles_labels()
index_item_title = labels.index('Item')
color_dict = {label: handle.get_color()
              for handle, label in zip(handles[1:index_item_title], labels[1:index_item_title])}

# loop through the items, assign color via the subscale of the item idem
for handle, label in zip(handles[index_item_title + 1:], labels[index_item_title + 1:]):
    handle.set_color(color_dict[subscale_dict[ label]])

# create a legend only using the items
ax.legend(handles[index_item_title + 1:], labels[index_item_title + 1:], title='Item',
          bbox_to_anchor=(1.03, 1.02), fontsize=10)

plt.tight_layout()
plt.show()

这篇关于如何在海运图例中组合色调和样式组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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