簡體   English   中英

熊貓groupby散點圖不改變顏色

[英]pandas groupby scatter plot not changing color

我有一個包含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()

散點圖

怎么了?

數據:

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'])

嘗試升級seaborn和matplotlib。 當matplotlib對是否恰好三個點指定了顏色感到困惑時,這發生在我身上(請參閱https://github.com/mwaskom/seaborn/issues/753 )。 我相信這在最新版本中已完全修復。

您也可以簡單地將顏色作為十六進制代碼而不是rgb值來傳遞。 sns.color_palette("Set2", 19).as_hex()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM