【问题标题】:Pandas Boxplot , also include most recent value with a markerPandas Boxplot ,还包括带有标记的最新值
【发布时间】:2021-06-29 21:31:17
【问题描述】:

我有一个箱线图,我想从具有 5 个不同分类变量(不同类型的油)的时间序列中显示。

如何使用标记显示最新值,显示在相关箱形图上。在我的示例中,我使用变量 maxDate 来显示每种油类型的最新值。

import pandas as pd
import seaborn as sns

# read Data Files, create data frame for all products
dfBr = pd.read_excel (r'\filepath.xlsx',
                   skiprows=1,
                   usecols=(0,1,13,14))
dfCb = pd.read_excel (r'\filepath.xlsx',
                   skiprows=1,
                   usecols=(0,1,13,14))
sns.set_style('whitegrid')
total = [dfBr,dfCb]
df = pd.concat(total)

df.columns =['Commodity', 'Date', 'mmLong', 'mmShort']
df.tail() 

df['Net_OI']=df['mmLong']-df['mmShort']
df['LS_Ratio']=df['mmLong']/df['mmShort']

df=df[df['Date'] > 180600]

df['Commodity'] = df['Commodity'].replace(['CRUDE OIL, LIGHT SWEET - NEW YORK MERCANTILE EXCHANGE',
                                           'ICE Brent Crude Futures - ICE Futures Europe',
                                           'CRUDE OIL, LIGHT SWEET-WTI - ICE FUTURES EUROPE',
                                           'GASOLINE BLENDSTOCK (RBOB)  - NEW YORK MERCANTILE EXCHANGE',
                                           '#2 HEATING OIL- NY HARBOR-ULSD - NEW YORK MERCANTILE EXCHANGE']
                                          ,['WTI',
                                            'BRN',
                                            'ICE',
                                            'RBOB',
                                            'HO'])
maxDate = df.Date.iloc[-1]

currentWTI = df.loc[ (df['Commodity'] == 'WTI') & (df['Date'] == maxDate)]
currentBrn = df.loc[ (df['Commodity'] == 'BRN') & (df['Date'] == maxDate)]
currentIce = df.loc[ (df['Commodity'] == 'ICE') & (df['Date'] == maxDate)]
currentRb = df.loc[ (df['Commodity'] == 'RBOB') & (df['Date'] == maxDate)]
currentHo = df.loc[ (df['Commodity'] == 'HO') & (df['Date'] == maxDate)]


fig, ax = plt.subplots(figsize=(8,8))
#sns.boxplot(x='Net_OI', y='Market_and_Exchange_Names', data=three_yr_df);
sns.boxplot(x=df.LS_Ratio, y=df.Commodity);

plt.scatter(currentBrn.LS_Ratio, 0,marker='*', s=350, color='orange');

目前最终结果看起来像这样,但我想显示所有 5 个项目的箱线图,并在 5 个项目的每个项目上都有一个 Star 标记。

任何帮助将不胜感激。

Boxplot with 1 marker

【问题讨论】:

    标签: pandas boxplot marker scatter


    【解决方案1】:

    由于没有提供数据,我以官方参考资料中的箱线图为例来对你的作业进行编码。关键是使用每个类别变量中的最大值作为放置星星的数组。

    import seaborn as sns
    import matplotlib.pyplot as plt
    
    sns.set_theme(style="whitegrid")
    tips = sns.load_dataset("tips")
    
    fig, ax = plt.subplots()
    ax = sns.boxplot(x="day", y="total_bill", data=tips)
    star = tips[['day','total_bill']].groupby('day').max()
    ax.scatter(star.index, star.total_bill, marker='*', s=350, color='orange')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2018-05-28
      • 2015-02-22
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多