【问题标题】:Using two types of grouping, one for plotting line one for color in matplotlib使用两种类型的分组,一种用于在 matplotlib 中绘制线,一种用于颜色
【发布时间】:2023-03-17 01:27:02
【问题描述】:

我对任何类型的编码都不熟悉,所以请多多包涵。 我使用df.groupby 将一组最初包含 33 列的数据绘制成一个绘图。 这样我就有了多行,每行代表特定样本随时间的变化。用我的方法,每条线都有一个独特的颜色。 我希望在“存储温度”列中具有相同值的线条在图中具有相同的颜色。

这是我目前的代码:

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame([['Glass', 'BK1', 150, 48, 15],
                   ['Metal', 'BK1', 150, 48, 23],
                   ['Glass', 'BK1', 120, 48, 12],
                   ['Glass', 'BK2', 120, 48, 13],
                   ['Glass', 'BK1', 150, 200, 18],
                   ['Metal', 'BK1', 150, 200, 26],
                   ['Glass', 'BK1', 120, 100, 17],
                   ['Glass', 'BK2', 120, 100, 14],
                   ['Glass', 'BK1', 150, 250, 20],
                   ['Metal', 'BK1', 150, 270, 27],
                   ['Glass', 'BK1', 120, 150, 25],
                   ['Glass', 'BK2', 120, 150, 16]],
                  columns=['Material', 'Colour', 'Storage Temperature',
                            'Storage Time', 'Result'])

groups = df.groupby(['Material', 'Colour', 'Storage Temperature'])

fig, ax = plt.subplots(figsize=(15, 9))

for element, group in groups:
        group.plot(x='Storage Time', y='Result', ax=ax,
                   marker= 'o',
                   linestyle='-',
                   ms=10, fontsize=20,
                   label='%s %s, %s°C'
                   % (group.iloc[0]['Material'],
                      group.iloc[0]['Colour'],
                      group.iloc[0]['Storage Temperature']))
plt.show()

这是我得到的:https://i.stack.imgur.com/OqUgY.png

我试过了

sns.lineplot('Storage Time', 'Result', data=groups,
             hue='Storage Temperature', estimator=None)

而不是group.plot,但我收到一个错误:无法访问“DataFrameGroupBy”对象的可调用属性“get”,请尝试使用“apply”方法

我怎样才能做到这一点?

【问题讨论】:

标签: python matplotlib colors pandas-groupby


【解决方案1】:

这是使用matplotlib colormaps 的快速修复:

import pandas as pd
from matplotlib import cm

groups = df.groupby(['Material', 'Colour', 'Storage Temperature'])
fig, ax = plt.subplots(figsize=(15, 9))

max_T = max(df['Storage Temperature'])
cmap = cm.get_cmap('viridis')
for element, group in groups:
    ax.plot(group['Storage Time'], group['Result'], marker = 'o', ms = 10, 
            label = element[0]+', '+element[1]+' '+element[2] + '°C', color = 
            cmap(element[2]/max_T))
plt.legend()
plt.show()

或者,编写一个为您选择颜色的小函数

def colorselector(temp):
    if temp == 150:
        return 'red'
    elif temp == 120:
        return 'blue'
    elif temp == ... :
        return 'green'
    ...

for element, group in groups:
    ax.plot(group['Storage Time'], group['Result'], marker = 'o', ms =10, 
    label = element[0]+', '+element[1]+' '+str(element[2]) +'°C',
    color = colorselector(element[2]))

plt.legend()
plt.show()

您的colorselector 也可以使用 cmap 来获得漂亮的颜色 :)

【讨论】:

    猜你喜欢
    • 2013-02-07
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2013-08-02
    • 2015-07-11
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    相关资源
    最近更新 更多