首页 > 解决方案 > 如何在 pyplot 图中突出显示某个值?

问题描述

我创建了三个高斯的简单 pyplot。现在,我想画一条从 x 轴到每个高斯峰值的虚线直线。这可能吗?

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

mu1 = 5
mu2 = 10
variance1 = 3
variance2 = 1
sigma1 = math.sqrt(variance1)
sigma2 = math.sqrt(variance2)

mu_combined = (variance2*mu1 + variance1*mu2)/(variance1 + variance2)
variance_combined = 1/(1/variance1 + 1/variance2)
sigma_combined = math.sqrt(variance_combined)

x = np.linspace(0,15,100)
plt.plot(x,norm.pdf(x, mu1, sigma1),'b')
plt.plot(x,norm.pdf(x, mu2, sigma2),'r')
plt.plot(x,norm.pdf(x, mu_combined, sigma_combined),'g')
plt.plot([5,5],[0,0.23],'b:')
plt.plot([10,10],[0,0.4],'r:')
plt.plot([8.7,8.7],[0,0.46],'g:')

ax = plt.gca()
ax.set_xlabel('Random Variable')
ax.set_ylabel('Probability')
plt.grid()
plt.show()

plt.savefig('Program_Path/Gaussians.svg', format='svg')

标签: pythonmatplotlib

解决方案


推荐阅读