【问题标题】:How to use whisker parameter of boxplot on violin plot for seaborn?如何在seaborn的小提琴图上使用箱线图的胡须参数?
【发布时间】:2018-01-19 03:58:07
【问题描述】:

seaborn 箱形图有 whis='range' 来绘制最小/最大异常值,但小提琴图在其文档中没有这个。如何在小提琴中使用箱线图参数?

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

a=[195.0, 245.0, 142.0, 237.0, 153.0, 238.0, 168.0, 145.0, 229.0, 138.0, 176.0, 116.0, 252.0, 148.0, 199.0, 162.0, 134.0, 163.0, 130.0, 339.0, 152.0, 208.0, 152.0, 192.0, 163.0, 249.0, 113.0, 176.0, 123.0, 189.0, 150.0, 207.0, 184.0, 153.0, 228.0, 153.0, 170.0, 118.0, 302.0, 197.0, 211.0, 159.0, 228.0, 147.0, 166.0, 156.0, 167.0, 147.0, 126.0, 155.0, 138.0, 159.0, 139.0, 111.0, 133.0, 134.0, 131.0, 156.0, 240.0, 207.0, 150.0, 207.0, 265.0, 151.0, 173.0, 157.0, 261.0, 186.0, 195.0, 158.0, 272.0, 134.0, 221.0, 131.0, 252.0, 148.0, 178.0, 206.0, 146.0, 217.0, 159.0, 190.0, 156.0, 172.0, 159.0, 141.0, 167.0, 168.0, 218.0, 191.0, 207.0, 164.0]

fig, axes = plt.subplots()

# Seaborn violin plot
#sns.violinplot(data=a, whis='range') doesn't work
sns.violinplot(data=a)

# Normal boxplot has full range, same in Seaborn boxplot
# axes.boxplot(a, whis='range')

plt.show()

【问题讨论】:

  • 您希望whis='range' 参数对小提琴图产生什么影响?
  • @ImportanceOfBeingErnest 我希望箱线图(在小提琴图中)的最小/最大胡须能够到达小提琴图的顶部和底部,这可以在普通箱线图上使用whis='range' 来实现。
  • 后期编辑,但我想说sns.violinplot(data=a, cut=0)时触摸小提琴情节的顶部和底部

标签: python matplotlib seaborn boxplot


【解决方案1】:

当然,解决方案可以是在 seaborn 小提琴图上叠加一个正常的箱线图,该箱线图确实有 whis='range' 参数可用。

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

a=[195.0, 245.0, 142.0, 237.0, 153.0, 238.0, 168.0, 145.0, 229.0, 138.0, 176.0, 116.0, 252.0, 148.0, 
   199.0, 162.0, 134.0, 163.0, 130.0, 339.0, 152.0, 208.0, 152.0, 192.0, 163.0, 249.0, 113.0, 176.0, 
   123.0, 189.0, 150.0, 207.0, 184.0, 153.0, 228.0, 153.0, 170.0, 118.0, 302.0, 197.0, 211.0, 159.0, 
   228.0, 147.0, 166.0, 156.0, 167.0, 147.0, 126.0, 155.0, 138.0, 159.0, 139.0, 111.0, 133.0, 134.0, 
   131.0, 156.0, 240.0, 207.0, 150.0, 207.0, 265.0, 151.0, 173.0, 157.0, 261.0, 186.0, 195.0, 158.0, 
   272.0, 134.0, 221.0, 131.0, 252.0, 148.0, 178.0, 206.0, 146.0, 217.0, 159.0, 190.0, 156.0, 172.0, 
   159.0, 141.0, 167.0, 168.0, 218.0, 191.0, 207.0, 164.0]

fig, axes = plt.subplots()

# Seaborn violin plot
sns.violinplot(data=a, color="#af52f4", inner=None, linewidth=0, saturation=0.5)

# Normal boxplot has full range, same in Seaborn boxplot
axes.boxplot(a, whis='range', positions=np.array([0]),
            showcaps=False,widths=0.06, patch_artist=True,
            boxprops=dict(color="indigo", facecolor="indigo"),
            whiskerprops=dict(color="indigo", linewidth=2),
            medianprops=dict(color="w", linewidth=2 ))

axes.set_xlim(-1,1)
plt.show()

【讨论】:

  • 我希望有一个更直观的方法,但这同样有效。
猜你喜欢
  • 2019-09-14
  • 2019-03-08
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 2016-08-20
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
相关资源
最近更新 更多