[英]Can't display the seaborn distplot
我正在尝试使用Python
的seaborn
模块制作直方图和密度图,并且在绘图上我还尝试在模式处绘制一条垂直线。 但是,生成的图不会显示任何直方图和/或密度曲线。 我的代码如下:
# a function to compute mode of the histograms shown in Figures `2` and `3` in the paper.
def compute_mode(layer_list):
ax = sns.distplot(layer_list, hist=False, kde=True, kde_kws={'linewidth': 2})
x = ax.lines[0].get_xdata()
y = ax.lines[0].get_ydata()
mode_idx = y.argmax()
mode_x = x[mode_idx]
plt.close()
return mode_x
# function to plot the histogram of the layer lists.
def make_density(layer_list, color):
# Plot formatting
plt.xlabel('Median Stn. MC-Loss')
plt.ylabel('Density')
# Draw the histogram and fit a density plot.
sns.distplot(layer_list, hist = True, kde = True,
kde_kws = {'linewidth': 2}, color=color)
# compute mode of the histogram.
mode_x = compute_mode(layer_list)
# draw a vertical line at the mode of the histogram.
plt.axvline(mode_x, color='blue', linestyle='dashed', linewidth=1.5)
plt.text(mode_x, 0.16, 'mode: {:.4f}'.format(mode_x))
layer_list = [ 1.0,2.0,3.0,4.0,2.0,3.0,1.0,6.0,10.0,2.0]
make_density(layer_list, 'green')
我认为问题出自plt.axvline(mode_x, color='blue', linestyle='dashed', linewidth=1.5)
和plt.text(mode_x, 0.16, 'mode: {:.4f}'.format(mode_x))
。
我在这里做错了什么?
谢谢,
主要问题是在compute_mode()
plt.close()
,它关闭了之前在make_density()
创建的make_density()
。 请注意, sns.distplot
主要是一个绘图函数,不应仅用于计算。 由于 kde 已经在make_density()
绘制, make_density()
ax.lines[0]
可以传递给compue_mode()
以提取曲线数据,而无需再次创建绘图。
其他一些说明:
distplot
已被弃用,取而代之的是两个函数: histplot
创建直方图,可选择使用 kde。 和displot
(没有“T”),它创建了一个直方图/kdeplots 网格。 除了名称混乱, kde_kws
的参数distplot
被称为line_kws
在histplot
,而kde_kws
在histplot
是用于那些计算KDE功能。 此外,kde 曲线始终使用与直方图相同的颜色。ax
,可用于其他格式。 在您的原始代码中,您在调用distplot
之前添加了一些标签,但由于distplot
也可能会更改设置,因此之后进行所有这些格式化调用会更安全。ax.set_xlabel()
和plt.xlabel()
之间的区别。import matplotlib.pyplot as plt
import seaborn as sns
# a function to compute mode of the histograms shown in Figures `2` and `3` in the paper.
def compute_mode(line_object):
x = line_object.get_xdata()
y = line_object.get_ydata()
mode_idx = y.argmax()
return x[mode_idx], y[mode_idx]
# function to plot the histogram of the layer lists.
def make_density(layer_list, color):
# Draw the histogram and fit a density plot.
ax = sns.histplot(layer_list, kde=True,
line_kws={'linewidth': 2}, color=color)
# compute mode of the histogram.
mode_x, mode_y = compute_mode(ax.lines[0])
# draw a vertical line at the mode of the histogram.
ax.axvline(mode_x, color='blue', linestyle='dashed', linewidth=1.5)
ax.text(mode_x, mode_y, 'mode: {:.4f}'.format(mode_x))
# Plot formatting
ax.set_xlabel('Median Stn. MC-Loss')
ax.set_ylabel('Density')
layer_list = [1.0, 2.0, 3.0, 4.0, 2.0, 3.0, 1.0, 6.0, 10.0, 2.0]
make_density(layer_list, 'green')
plt.show()
在计算模式中调用 seaborn 的 distplot 而不指定与绘图compute_mode
的斧头。 您可以简单地用以下代码替换compute_mode
:
def compute_mode(layer_list):
dummy_fig, dummy_ax = plt.subplots()
ax = sns.distplot(layer_list, hist=False, kde=True, kde_kws={'linewidth': 2}, ax=dummy_ax)
x = ax.lines[0].get_xdata()
y = ax.lines[0].get_ydata()
mode_idx = y.argmax()
mode_x = x[mode_idx]
plt.close()
return mode_x
即使此解决方法有效,请考虑使用专用工具(如scipy 的 gaussian_kde )计算模式。 它可以防止你搞乱图形库来做数学题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.