我有一个生成以下数字的python代码。我想用椭圆做一个注释来围绕曲线,如图中所示。
N.B。该图是使用MATLAB生成的,我无法在python-matplotlib中完成。 感谢。
答案 0 :(得分:5)
您可以尝试以下方法来定位椭圆:选择一个x坐标并计算在该坐标处包含所提供函数列表所需的椭圆高度。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse
x = np.linspace(1,10,1000)
flogs = [lambda x, a=a: np.log(a * x) for a in range(1,4)]
fexps = [lambda x, a=a: np.exp(a * x) for a in [0.18,0.2]]
funcs = flogs + fexps
fig = plt.figure()
ax = fig.add_subplot(111)
for func in funcs:
ax.plot(x,func(x))
def add_ellipse(funcs, x):
# the y-coordinate of the center of our ellipse:
y = np.mean([funcs[i](x) for i in range(len(funcs))])
# fix the width of the ellipse to this value:
w = 0.4
# find the height of the ellipse needed, and pad a bit:
h = np.ptp([funcs[i](x) for i in range(len(funcs))]) * 1.5
e = Ellipse(xy=(x,y), width=w, height=h, angle=0)
e.set_facecolor('none')
ax.add_artist(e)
add_ellipse(fexps, 8.5)
add_ellipse(flogs, 8)
plt.show()