如何用线连接箱线图的平均值
How to connect boxplots mean values with a line
我有以下箱形图代码,但不明白如何将平均值与一条线连接起来。
有什么建议么?谢谢
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')
您可以使用 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()
您还可以使用其他估算器,但默认情况下它会估算均值。
我有以下箱形图代码,但不明白如何将平均值与一条线连接起来。 有什么建议么?谢谢
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')
您可以使用 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()
您还可以使用其他估算器,但默认情况下它会估算均值。