pandas groupby散点图不改变颜色

时间:2017-09-28 12:42:23

标签: python pandas matplotlib group-by

我有一个包含4列的数据集。 2列是点的标签(['Drug','Layer']),2是coordiantes点(x ='Nodes',y ='CoreIndex')。 药物的基数为2,层数为15.所以我有30分的情节。我想要不同颜色的不同层的点。

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
fig,ax= plt.subplots(figsize=[10,5])
rgb_values = sns.color_palette("Set2", 19)

for name,group in data_pos.groupby(['Drug','Layer']):
    if 'lsd' in name:
       group.plot(x='Nodes', y='CoreIndex', kind='scatter', ax=ax, label=name, c=rgb_values[name[1]+3], marker='s', s=100)
else:
    group.plot(x='Nodes', y='CoreIndex', kind='scatter', ax=ax, label=name, c=rgb_values[name[1]+3], s=100)
plt.legend()
plt.show()

scatter plot

出了什么问题?

数据:

data_pos = pd.DataFrame([['lsd', 1L, 21L, 100L],
       ['lsd', 10L, 14L, 76L],
       ['lsd', 11L, 18L, 126L],
       ['lsd', 12L, 28L, 70L],
       ['lsd', 13L, 12L, 82L],
       ['lsd', 14L, 23L, 87L],
       ['lsd', 15L, 19L, 71L],
       ['lsd', 2L, 20L, 76L],
       ['lsd', 3L, 16L, 59L],
       ['lsd', 4L, 16L, 118L],
       ['lsd', 5L, 14L, 109L],
       ['lsd', 6L, 10L, 134L],
       ['lsd', 7L, 16L, 96L],
       ['lsd', 8L, 11L, 148L],
       ['lsd', 9L, 18L, 130L],
       ['lsd', 1L, 22L, 91L],
       ['lsd', 10L, 14L, 99L],
       ['lsd', 11L, 29L, 93L],
       ['lsd', 12L, 37L, 125L],
       ['lsd', 13L, 13L, 139L],
       ['lsd', 14L, 19L, 143L],
       ['lsd', 15L, 18L, 78L],
       ['lsd', 2L, 27L, 68L],
       ['lsd', 3L, 14L, 101L],
       ['lsd', 4L, 15L, 90L],
       ['lsd', 5L, 16L, 112L],
       ['lsd', 6L, 11L, 50L],
       ['lsd', 7L, 15L, 115L],
       ['lsd', 8L, 13L, 139L],
       ['lsd', 9L, 17L, 67L],
       ['lsd', 1L, 15L, 82L],
       ['lsd', 10L, 13L, 133L],
       ['lsd', 11L, 19L, 98L],
       ['lsd', 12L, 27L, 104L],
       ['lsd', 13L, 11L, 90L],
       ['lsd', 14L, 25L, 73L],
       ['lsd', 15L, 17L, 126L],
       ['lsd', 2L, 22L, 62L],
       ['lsd', 3L, 14L, 149L],
       ['lsd', 4L, 16L, 108L],
       ['lsd', 5L, 13L, 91L],
       ['lsd', 6L, 10L, 132L],
       ['lsd', 7L, 15L, 136L],
       ['lsd', 8L, 15L, 65L],
       ['lsd', 9L, 14L, 142L],
       ['pcb', 1L, 17L, 104L],
       ['pcb', 10L, 13L, 68L],
       ['pcb', 11L, 23L, 71L],
       ['pcb', 12L, 20L, 79L],
       ['pcb', 13L, 11L, 96L],
       ['pcb', 14L, 15L, 74L],
       ['pcb', 15L, 16L, 63L],
       ['pcb', 2L, 13L, 34L],
       ['pcb', 3L, 18L, 67L],
       ['pcb', 4L, 18L, 70L],
       ['pcb', 5L, 15L, 143L],
       ['pcb', 6L, 14L, 48L],
       ['pcb', 7L, 16L, 125L],
       ['pcb', 8L, 15L, 94L],
       ['pcb', 9L, 19L, 69L],
       ['pcb', 1L, 15L, 72L],
       ['pcb', 10L, 12L, 59L],
       ['pcb', 11L, 21L, 66L],
       ['pcb', 12L, 17L, 71L],
       ['pcb', 13L, 14L, 42L],
       ['pcb', 14L, 17L, 93L],
       ['pcb', 15L, 13L, 148L],
       ['pcb', 2L, 12L, 72L],
       ['pcb', 3L, 15L, 137L],
       ['pcb', 4L, 13L, 146L],
       ['pcb', 5L, 13L, 137L],
       ['pcb', 6L, 12L, 111L],
       ['pcb', 7L, 14L, 131L],
       ['pcb', 8L, 12L, 113L],
       ['pcb', 9L, 16L, 134L],
       ['pcb', 1L, 14L, 64L],
       ['pcb', 10L, 12L, 83L],
       ['pcb', 11L, 23L, 79L],
       ['pcb', 12L, 15L, 137L],
       ['pcb', 13L, 14L, 69L],
       ['pcb', 14L, 28L, 89L],
       ['pcb', 15L, 20L, 83L],
       ['pcb', 2L, 13L, 56L],
       ['pcb', 3L, 23L, 58L],
       ['pcb', 4L, 16L, 77L],
       ['pcb', 5L, 14L, 111L],
       ['pcb', 6L, 14L, 51L],
       ['pcb', 7L, 14L, 122L],
       ['pcb', 8L, 12L, 58L],
       ['pcb', 9L, 17L, 54L]],columns=['Drug','Layer','Nodes','CoreIndex'])

1 个答案:

答案 0 :(得分:0)

尝试升级seaborn和matplotlib。当matplotlib对三个点是否指定了一个颜色感到困惑时,这发生在我身上(见https://github.com/mwaskom/seaborn/issues/753)。我相信这在最新版本中已完全修复。

您也可以简单地将颜色作为十六进制代码而不是rgb值传递。 (sns.color_palette("Set2", 19).as_hex()