首页 文章

用Seaborn,Pandas绘制高低

提问于
浏览
1

我有一个pandas数据框,按类别三个数据点:mean,max,min .

我想绘制这些,使得均值是一个点,最大/最小值是一条线 . 类似于股票中的高/低/收盘图,甚至只是误差线 .

为了对话,假设我的代码看起来像

df = pd.DataFrame({'day': ['M', 'T', 'W', 'F'],
              'foo' : [1,2,3,4],
              'foo_max' : [5,5,6,7],
              'foo_min' : [0,1,1,1]})

sns.stripplot(df.day, df.foo, color='black')
plt.show()

1 回答

  • 1

    你可以这样做:

    df.set_index('day', inplace=True)
    
    # tsplot with error bars
    ax = sns.tsplot([df['foo_max'], df['foo_min']], err_style="ci_bars", 
                    interpolate=False, color='g')
    
    ax.set_xticks(np.arange(0, df.shape[0]))
    ax.set_xticklabels(df.index) 
    ax.set_ylim(0, df.values.max()+1)
    sns.plt.show()
    

    enter image description here

相关问题