科学的pdf评估得出矛盾的价值

时间:2018-10-27 15:28:30

标签: python matplotlib plot scipy statistics

我正试图写下一个图表,表明无偏估计器并不总是最佳估计器。这是我为获得美观图片而编写的代码:

# Set up the plot
fig, ax = plt.subplots()
# Create some data
y = np.linspace(-10, 10, 1000)
# Plot two normals, one centered around 0 but with large variance
ax.plot(y, norm.pdf(y, scale=3), 'k-', label=r"pdf of $\hat{\theta_1}$")
# One centered around 1 with small variance
ax.plot(y, norm.pdf(y, loc=1, scale=1), 'r--', label=r"pdf of $\hat{\theta_2}$")
ax.legend()
# Remove left, right and top axis, remove y axis labels and ticks
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.tick_params(axis='y', which='both', left=False, right=False, labelleft=False)
# Remove x axis ticks and labels, keep only one at x=0
ax.set_xticks([0])
ax.set_xticklabels([r"$\theta$"])
# Plot vertical line at x=0 from y=0 to the value of the first pdf
ax.axvline(x=0, ymin=0, ymax=norm.pdf(0, scale=3), linestyle=":")
# Plot second vertical line for second normal distribution
ax.axvline(x=1, ymin=0, ymax=norm.pdf(1, loc=1, scale=1), linestyle=":")
# Remove margins so that pdfs lie on the axis
ax.margins(0)
plt.show(block=True)

基本上我想绘制两个正态分布,一个以0为中心但方差较大,另一个以1为中心并方差较小。然后,我想添加两条垂直线。一条线穿过x = 0并在x = 0处达到第一正态分布的值,第二条穿过x = 1并在x = 1处达到第二正态分布的值。但是,这些行要小得多,我不知道为什么!

plot

我唯一的猜测是,由于这些都是连续的pdf,因此,如果我一次评估它们,scipy会做一些奇怪的事情。

进口

我忘了提及我的进口商品了,我在这里包括它们,这样您就可以拥有MWE

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm

1 个答案:

答案 0 :(得分:1)

查看axvline的文档:

ymax : scalar, optional, default: 1
 Should be between 0 and 1, 0 being the bottom of the plot, 1 the top of the plot.

您的线是在 data 坐标而非轴坐标中定义的。您需要改用vlines

# Plot vertical line at x=0 from y=0 to the value of the first pdf
ax.vlines(x=0, ymin=0, ymax=norm.pdf(0, scale=3), linestyle=":")
# Plot second vertical line for second normal distribution
ax.vlines(x=1, ymin=0, ymax=norm.pdf(1, loc=1, scale=1), linestyle=":")

fixed plot