我想在一些指数分布图中插入一些箭头:
import pylab as pl
import numpy as np
def gauss2d(x,sigma):
return (1/np.sqrt(2*np.pi*sigma ))*np.exp(-1/2*(x/sigma)**2 )
def draw_arrow(zero, sigma, function):
startx = zero
print startx,function(sigma, sigma)
arr = pl.Arrow(startx,function(startx+sigma, sigma), sigma,0,fc="k",ec="k")
ax = pl.gca()
ax.add_patch(arr)
def plot_gauss2d():
x = np.mgrid[115:135:100j]
#x=np.array(zip(range(5)),dtype=float)
sigma = 1
off=1.0
pl.plot(x,gauss2d(x-126.21,3.56), 'b-')
draw_arrow(126.21, 3.56, gauss2d)
pl.plot(x,gauss2d(x-126.71,4.57), 'b-')
pl.plot(x,gauss2d(x-120.64,3.5), 'b-')
pl.ylabel('frequency')
pl.xlabel('ppm of N')
pl.title
pl.show()
def main():
plot_gauss2d()
if __name__ == "__main__":
main()
不知何故,我似乎无法使箭头正确。我基本上希望拥有的是这样的:
我根本无法弄清楚的是如何将箭头直接设置到我想要的位置。它应标记正确高度的标准偏差点。整个事情当然应该产生多个指数曲线。
答案 0 :(得分:2)
箭头的问题在于它使用figure
坐标与data
坐标相比较。因此,正如@Paul建议的那样,您可以使用注释,如
import pylab as pl
import numpy as np
def gauss2d(x,sigma):
return (1/np.sqrt(2*np.pi*sigma ))*np.exp(-1/2*(x/sigma)**2 )
def markParameters(m,s):
p1=gauss2d(s,s)
p2=gauss2d(0,s)
pl.annotate("", xy=(m-s, p1), xycoords='data', xytext=(m+s, p1), textcoords='data', arrowprops=dict(arrowstyle="<->", connectionstyle="arc3"),)
pl.text(m,p1,'sigma',horizontalalignment='center',verticalalignment='top')
pl.annotate("", xy=(m, 0), xycoords='data', xytext=(m, p2), textcoords='data', arrowprops=dict(arrowstyle="<->", connectionstyle="arc3"),)
pl.text(m,p2*0.75,'mean',horizontalalignment='right',verticalalignment='center',rotation=90)
def plot_gauss2d():
x = np.mgrid[115:135:100j]
#x=np.array(zip(range(5)),dtype=float)
m,s=126,3.56
pl.plot(x,gauss2d(x-m,s), 'b-')
markParameters(m,s)
pl.ylabel('frequency')
pl.xlabel('ppm of N')
pl.title
pl.show()
def main():
plot_gauss2d()
if __name__ == "__main__":
main()
答案 1 :(得分:1)
查看此演示版的annotate
方法:
http://matplotlib.sourceforge.net/examples/pylab_examples/annotation_demo.html
这应该照顾你需要的东西。