0

I have following code for a boxplot, but don't understand how to connect mean values with a line. any suggestions? thanks

df4 = df3['BANK 2 5Y'].resample('M').first().diff().shift(freq='-1M')

df4 = df4.rename_axis('index1').reset_index()
df4['Mnd']=df4['index1'].dt.month

fig, ax = plt.subplots()
#sns.set(rc={'figure.figsize':(20,15)}, style='whitegrid')

flierprops = dict(marker='*', markerfacecolor='black', markersize=2,  markeredgecolor='black')


sns.boxplot(x = df4['Mnd'], y = df4['BANK 2 5Y'], data = df4, showfliers = True, flierprops=flierprops, showmeans = True, color = 'lightblue',meanprops={"marker":"o",
                       "markerfacecolor":"red", 
                       "markeredgecolor":"black",
                      "markersize":"6"},ax = ax, whis = [0,100])
ax.yaxis.grid(True)
plt.grid(b = True, axis = 'y', ls = '--', linewidth = 0.25, color = 'black')

enter image description here

NolsN
  • 25
  • 4

1 Answers1

1

You could use pointplot:

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

# Fake data.
df4 = pd.DataFrame()
df4['Mnd'] = [1] * 100 + [2] * 100 + [3] * 100 + [4] * 100
df4['BANK 2 5Y'] = np.r_[np.random.random((100)), np.random.random((100))*20, 
                         np.random.random((100))*40, np.random.random((50))*50, np.random.random((50))*5]

fig, ax = plt.subplots()

flierprops = dict(marker='*', markerfacecolor='black', markersize=2,  markeredgecolor='black')


sns.boxplot(x = df4['Mnd'], y = df4['BANK 2 5Y'], data = df4, showfliers = True, flierprops=flierprops, showmeans = True, color = 'lightblue',meanprops={"marker":"o",
                       "markerfacecolor":"red", 
                       "markeredgecolor":"black",
                      "markersize":"7"},ax = ax, whis = [0,100])
ax.yaxis.grid(True)
plt.grid(b = True, axis = 'y', ls = '--', linewidth = 0.25, color = 'black')

# New code here.
sns.pointplot(x=df4['Mnd'], y=df4['BANK 2 5Y'], data=df4, ci=None, color='r', scale=0.8)
plt.show()

enter image description here

You can also use other estimators with it, but it estimates the mean by default.

user2246849
  • 4,217
  • 1
  • 12
  • 16