如何使用不同于Python中绘制的数据框为Seaborn箱形图着色?

时间:2019-11-13 00:11:42

标签: python seaborn boxplot

我正在尝试使用Seaborn创建箱形图,该箱形图具有来自一个数据集的数据,但是根据不同的数据集进行着色。以下是一个较小的数据集作为示例。我实际使用的数据集要大得多。

  1. 导入库:
CONTAINER ID        IMAGE                                     COMMAND                  CREATED             STATUS                   PORTS                          NAMES
caa0b94c1d98        qnib/plain-kafka:1.1.0                    "/usr/local/bin/entr…"   3 minutes ago       Up 3 minutes (healthy)                                  broker.2.3fij6pt90qt9sb9aco0i2dpys
b888cb6f783a        qnib/plain-kafka:1.1.0                    "/usr/local/bin/entr…"   3 minutes ago       Up 3 minutes (healthy)                                  broker.3.xqmjnfnfg7ha46lf6drlrq4ki
dcdda2d778c2        qnib/plain-kafka:1.1.0                    "/usr/local/bin/entr…"   3 minutes ago       Up 3 minutes (healthy)                                  broker.1.gtgluxt6q58z2irzgfmu969ba
843def0b24fb        qnib/plain-zkui                           "/usr/local/bin/entr…"   3 minutes ago       Up 3 minutes (healthy)                                  zkui.1.7zks618eae8sp4woc7araydix
d7ced19be88c        qnib/plain-kafka-manager:2018-04-25       "/usr/local/bin/entr…"   3 minutes ago       Up 3 minutes (healthy)                                  manager.1.jdu5gnprhr4d982vz50511rhg
a67ac962e682        qnib/plain-zookeeper:2018-04-25           "/usr/local/bin/entr…"   3 minutes ago       Up 3 minutes (healthy)   2181/tcp, 2888/tcp, 3888/tcp   zookeeper.1.xar7cmdgozdj79orow0bmj3ev
880121f2fee5        qnib/golang-kafka-producer:2018-05-01.5   "kafka-producer"         3 minutes ago       Up 3 minutes (healthy)                                  producer.2.hety8za590v1twdgj2byvrmse
b6487d29812e        qnib/golang-kafka-producer:2018-05-01.5   "kafka-producer"         3 minutes ago       Up 3 minutes (healthy)                                  producer.1.5oz02c8cw5oefc97xbarq5qoa
8b3a81905e90        qnib/golang-kafka-producer:2018-05-01.5   "kafka-producer"         3 minutes ago       Up 3 minutes (healthy)                                  producer.3.p8uh3hzr22fgm7u4gl1p3fiyw

  1. 创建数据集
import numpy as np
import pandas as pd
import seaborn as sns
  1. 制作箱形图
data = ([[0.038095,0.259664,-0.016144],
         [0.070850,0.533989,0.221025],
         [0.010452,0.108146,0.007267],
         [0.033338,0.006664,0.160160],
         [0.005897,0.060313,-0.001070],
         [0.089018,0.002074,0.409608],
         [-0.010612,0.006957,0.331146],
         [-0.002889,0.005181,0.928332]])
dataset = pd.DataFrame(data,columns=['A','B','C'])
dataset

Boxplot figure

  1. 这是我要用来为箱线图着色的数据集。基本上,我希望A和B为红色(“否”),而C为绿色(“是”)。
sns.boxplot(data=dataset)

那么,如何根据类型数据集为箱形图着色?我需要合并数据集吗?任何/所有帮助表示赞赏!谢谢。

1 个答案:

答案 0 :(得分:0)

palette中使用boxplot关键字参数:

sns.boxplot(data=dataset, palette=['r', 'r', 'g'])

或从type_dataset对象中获取元数据:

cmap = {'Yes': 'green', 'No': 'red'}
sns.boxplot(data=dataset, palette=[cmap[v] for v in type_dataset[1]])