使用 Seaborn 绘制高低图,Pandas

Plot high-low with Seaborn, Pandas

我有一个 pandas 数据框,其中包含三个数据点类别:平均值、最大值、最小值。

我想绘制这些图,使平均值是一个点,max/min 是一条线。类似于股票中的 high/low/close 图表,甚至只是误差线。

为了便于交流,假设我的代码如下所示

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()

你可以这样做:

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()