【发布时间】:2016-10-28 14:00:31
【问题描述】:
我有一个概率密度函数,我只能评估对数而不会遇到数字问题。我有一个直方图,我想在同一个画布上绘制。但是,对于直方图,我需要选项log=True 将其绘制为对数刻度,而对于函数,我只能直接获得值的对数。如何在同一画布上绘制两者?
请查看此 MWE 以说明问题:
import matplotlib.pyplot as plt
import random
import math
import numpy as np
sqrt2pi = math.sqrt(2*math.pi)
def gauss(l):
return [ 1/sqrt2pi * math.exp(-x*x) for x in l]
def loggauss(l):
return [ -math.log(sqrt2pi) -x*x for x in l ]
# just fill a histogram
h = [ random.gauss(0,1) for x in range(0,1000) ]
plt.hist(h,bins=21,normed=True,log=True)
# this works nicely
xvals = np.arange(-4,4,0.1)
plt.plot(xvals,gauss(xvals),"-k")
# but I would like to plot this on the same canvas:
# plt.plot(xvals,loggauss(xvals),"-r")
plt.show()
有什么建议吗?
【问题讨论】:
标签: python matplotlib