上面带有Boxplot的直方图 [英] Histogram with Boxplot above in Python
问题描述
我想绘制一个直方图,其箱形图出现在直方图的顶部,显示Q1,Q2和Q3以及异常值.示例电话如下.(我正在使用Python和Pandas)
Hi I wanted to draw a histogram with a boxplot appearing the top of the histogram showing the Q1,Q2 and Q3 as well as the outliers. Example phone is below. (I am using Python and Pandas)
我已经使用 matplotlib.pyplot
检查了几个示例,但几乎没有一个好的示例.我还希望直方图曲线如下图所示.
I have checked several examples using matplotlib.pyplot
but hardly came out with a good example. And I also wanted to have the histogram curve appearing like in the image below.
我还尝试了 seaborn
,它为我提供了形状线和直方图,但是没有找到与上面的boxpot合并的方法.
I also tried seaborn
and it provided me the shape line along with the histogram but didnt find a way to incorporate with boxpot above it.
有人可以帮助我在 matplotlib.pyplot
上使用此代码,还是使用 pyplot
can anyone help me with this to have this on matplotlib.pyplot
or using pyplot
推荐答案
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")
x = np.random.randn(100)
f, (ax_box, ax_hist) = plt.subplots(2, sharex=True,
gridspec_kw={"height_ratios": (.15, .85)})
sns.boxplot(x, ax=ax_box)
sns.distplot(x, ax=ax_hist)
ax_box.set(yticks=[])
sns.despine(ax=ax_hist)
sns.despine(ax=ax_box, left=True)
这篇关于上面带有Boxplot的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!