在 python 中的矩形边缘仅绘制 box-plot 没有尾巴的最大值和最小值
Drawing box-plot without tails only max and min on the edges of the rectangle in python
我想为以下数据集绘制 box-plot,但我不需要尾巴。我需要一个边上有 max 和 min 的矩形。顺便说一句,它不一定是矩形,也可以是粗线
请帮忙。
谢谢。
import seaborn as sns
import pandas as pd
df=pd.DataFrame({'grup':['a','a','a','a','b','b','b','c','c','c','c','c','c','c'],'X1':
[10,9,12,5,20,43,28,40,65,78,65,98,100,150]})
df
ax = sns.boxplot(x="grup", y="X1", data=df, palette="Set3")
您可以创建条形图,使用最小值作为 'bottom',使用最大值和最小值之差作为高度。
请注意,条形图有一个“粘性”底部,将 y 轴的最低点固定到最低的条形。作为补救,我们可以改变ylim。
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame({'grup': ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c'],
'X1': [10, 9, 12, 5, 20, 43, 28, 40, 65, 78, 65, 98, 100, 150]})
grups = np.unique(df['grup'])
bottoms = [df[df['grup'] == g]['X1'].min() for g in grups]
heights = [df[df['grup'] == g]['X1'].max() - g_bottom for g, g_bottom in zip(grups, bottoms)]
ax = sns.barplot(x=grups, y=heights, bottom=bottoms, palette="Set3", ec='black')
# for reference, show where the values are; leave this line out for the final plot
sns.stripplot(x='grup', y='X1', color='red', s=10, data=df, ax=ax)
ax.set_xlabel('grup') # needed because the barplot isn't directly using a dataframe
ax.set_ylabel('X1')
ax.set_ylim(ymin=0)
更新:添加最小值和最大值:
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame({'grup': ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c'],
'X1': [10, 9, 12, 5, 20, 43, 28, 40, 65, 78, 65, 98, 100, 150]})
grups = np.unique(df['grup'])
bottoms = np.array([df[df['grup'] == g]['X1'].min() for g in grups])
tops = np.array([df[df['grup'] == g]['X1'].max() for g in grups])
ax = sns.barplot(x=grups, y=tops - bottoms, bottom=bottoms, palette="Set3", ec='black')
ax.set_xlabel('grup') # needed because the barplot isn't directly using a dataframe
ax.set_ylabel('X1')
ax.set_ylim(ymin=0)
for i, (grup, bottom, top) in enumerate(zip(grups, bottoms, tops)):
ax.text(i, bottom, f'\n{bottom}', ha='center', va='center')
ax.text(i, top, f'{top}\n', ha='center', va='center')
我想为以下数据集绘制 box-plot,但我不需要尾巴。我需要一个边上有 max 和 min 的矩形。顺便说一句,它不一定是矩形,也可以是粗线
请帮忙。
谢谢。
import seaborn as sns
import pandas as pd
df=pd.DataFrame({'grup':['a','a','a','a','b','b','b','c','c','c','c','c','c','c'],'X1':
[10,9,12,5,20,43,28,40,65,78,65,98,100,150]})
df
ax = sns.boxplot(x="grup", y="X1", data=df, palette="Set3")
您可以创建条形图,使用最小值作为 'bottom',使用最大值和最小值之差作为高度。
请注意,条形图有一个“粘性”底部,将 y 轴的最低点固定到最低的条形。作为补救,我们可以改变ylim。
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame({'grup': ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c'],
'X1': [10, 9, 12, 5, 20, 43, 28, 40, 65, 78, 65, 98, 100, 150]})
grups = np.unique(df['grup'])
bottoms = [df[df['grup'] == g]['X1'].min() for g in grups]
heights = [df[df['grup'] == g]['X1'].max() - g_bottom for g, g_bottom in zip(grups, bottoms)]
ax = sns.barplot(x=grups, y=heights, bottom=bottoms, palette="Set3", ec='black')
# for reference, show where the values are; leave this line out for the final plot
sns.stripplot(x='grup', y='X1', color='red', s=10, data=df, ax=ax)
ax.set_xlabel('grup') # needed because the barplot isn't directly using a dataframe
ax.set_ylabel('X1')
ax.set_ylim(ymin=0)
更新:添加最小值和最大值:
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame({'grup': ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c'],
'X1': [10, 9, 12, 5, 20, 43, 28, 40, 65, 78, 65, 98, 100, 150]})
grups = np.unique(df['grup'])
bottoms = np.array([df[df['grup'] == g]['X1'].min() for g in grups])
tops = np.array([df[df['grup'] == g]['X1'].max() for g in grups])
ax = sns.barplot(x=grups, y=tops - bottoms, bottom=bottoms, palette="Set3", ec='black')
ax.set_xlabel('grup') # needed because the barplot isn't directly using a dataframe
ax.set_ylabel('X1')
ax.set_ylim(ymin=0)
for i, (grup, bottom, top) in enumerate(zip(grups, bottoms, tops)):
ax.text(i, bottom, f'\n{bottom}', ha='center', va='center')
ax.text(i, top, f'{top}\n', ha='center', va='center')