使用 relplot 绘制导致错误的 pandas 数据帧

Use relplot to plot a pandas dataframe leading to error

提问人:user308827 提问时间:11/8/2023 最后编辑:Trenton McKinneyuser308827 更新时间:11/13/2023 访问量:85

问:

我想绘制以下数据帧:

import pandas as pd
import numpy as np
import seaborn as sns

data = {'Description': {0: 'Growing degree days (sum of Tmean > 4 C)', 1: 'Maximum number of consecutive frost days (Tmin < 0 C)', 2: 'Number of Frost Days (Tmin < 0C)', 3: 'Heating degree days (sum of Tmean < 17 C)', 4: 'Number of sharp Ice Days (Tmax < 0C)', 5: 'Cold-spell duration index', 6: 'Percentage of days when Tmean < 10th percentile', 7: 'Percentage of days when Tmin < 10th percentile', 8: 'Minimum daily maximum temperature', 9: 'Minimum daily minimum temperature'},
        'CEI': {0: 960.5901447, 1: 3.0, 2: 3.0, 3: 307.55743, 4: 0.0, 5: 0.0, 6: 8.0, 7: 7.0, 8: 7.897848629, 9: -3.803963957},
        'Country': {0: 'Argentina', 1: 'Argentina', 2: 'Argentina', 3: 'Argentina', 4: 'Argentina', 5: 'Argentina', 6: 'Argentina', 7: 'Argentina', 8: 'Argentina', 9: 'Argentina'}, 'Region': {0: 'Buenos Aires', 1: 'Buenos Aires', 2: 'Buenos Aires', 3: 'Buenos Aires', 4: 'Buenos Aires', 5: 'Buenos Aires', 6: 'Buenos Aires', 7: 'Buenos Aires', 8: 'Buenos Aires', 9: 'Buenos Aires'},
        'Area': {0: 7107272, 1: 7107272, 2: 7107272, 3: 7107272, 4: 7107272, 5: 7107272, 6: 7107272, 7: 7107272, 8: 7107272, 9: 7107272},
        'Crop': {0: 'Maize', 1: 'Maize', 2: 'Maize', 3: 'Maize', 4: 'Maize', 5: 'Maize', 6: 'Maize', 7: 'Maize', 8: 'Maize', 9: 'Maize'},
        'Season': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1},
        'Method': {0: 'phenological_stages', 1: 'phenological_stages', 2: 'phenological_stages', 3: 'phenological_stages', 4: 'phenological_stages', 5: 'phenological_stages', 6: 'phenological_stages', 7: 'phenological_stages', 8: 'phenological_stages', 9: 'phenological_stages'},
        'Stage': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1},
        'Harvest Year': {0: 2020, 1: 2020, 2: 2020, 3: 2020, 4: 2020, 5: 2020, 6: 2020, 7: 2020, 8: 2020, 9: 2020},
        'Index': {0: 'GD4', 1: 'CFD', 2: 'FD', 3: 'HD17', 4: 'ID', 5: 'CSDI', 6: 'TG10p', 7: 'TN10p', 8: 'TXn', 9: 'TNn'},
        'Type': {0: 'Cold', 1: 'Cold', 2: 'Cold', 3: 'Cold', 4: 'Cold', 5: 'Cold', 6: 'Cold', 7: 'Cold', 8: 'Cold', 9: 'Cold'},
        'percentiles': {0: np.nan, 1: np.nan, 2: np.nan, 3: np.nan, 4: np.nan, 5: 10.0, 6: 10.0, 7: 10.0, 8: np.nan, 9: np.nan},
        'Z-Score CEI': {0: 0.095693962, 1: 0.875995674, 2: -0.06780635, 3: 0.218503017, 4: np.nan, 5: np.nan, 6: -0.230243454, 7: -0.42505904, 8: -1.87248784, 9: -1.119380721},
        'Category CEI': {0: 'Average', 1: 'Good', 2: 'Average', 3: 'Average', 4: np.nan, 5: np.nan, 6: 'Average', 7: 'Average', 8: 'Failure', 9: 'Poor'},
        'Area (ha)': {0: 2472520, 1: 2472520, 2: 2472520, 3: 2472520, 4: 2472520, 5: 2472520, 6: 2472520, 7: 2472520, 8: 2472520, 9: 2472520},
        'Production (tn)': {0: 15595357, 1: 15595357, 2: 15595357, 3: 15595357, 4: 15595357, 5: 15595357, 6: 15595357, 7: 15595357, 8: 15595357, 9: 15595357},
        'Yield (tn per ha)': {0: 8.109, 1: 8.109, 2: 8.109, 3: 8.109, 4: 8.109, 5: 8.109, 6: 8.109, 7: 8.109, 8: 8.109, 9: 8.109}}
df = pd.DataFrame.from_dict(data)

我这样做了:

df['Stage'] = df['Stage'].astype('category')
sns.relplot(data=df, x='Z-Score CEI', y='Stage', col='Index', kind='line', ax=ax, facet_kws={'sharey': True})

但是,我收到此错误: raise KeyError(key) from err KeyError: 'x'

最终,我希望图上的 x 轴变窄,因为我期望有大量列 (~50)

蟒蛇 Seaborn Relplot

评论

1赞 Suraj Shourie 11/9/2023
您的代码对我有用,没有错误,您使用的是哪个版本的 seaborn?
0赞 user308827 11/9/2023
0.12.2,你的版本?
0赞 Suraj Shourie 11/9/2023
0.11.2,我不认为重新绘制在
1赞 Trenton McKinney 11/13/2023
不要使用 in ,这是一个没有 .鉴于最小可重现示例,此问题在ax=ax.relplotax=seaborn v0.13.0

答:

0赞 Christian Karcher 11/9/2023 #1

我在 seaborn 0.12.2 中遇到同样的错误

此 KeyError 的原因是相应列中的 nan 值。(例如,参见这个开放的Seaborn问题Z-Score CEI)

要消除此错误,您可以在绘制数据之前清理数据。暴力破解方式是 df = df.dropna(),但在您的情况下,这为我产生了空图,因此这里似乎需要更复杂的清理来避免此错误并仍然生成有意义的图。