Python 如何在matplotlib中使用注释环绕曲线?
我有一个python代码,它可以生成下面的图。如图所示,我想用椭圆对曲线进行注释 注意:该图是使用MATLAB生成的,我无法在python matplotlib中完成。Python 如何在matplotlib中使用注释环绕曲线?,python,matplotlib,Python,Matplotlib,我有一个python代码,它可以生成下面的图。如图所示,我想用椭圆对曲线进行注释 注意:该图是使用MATLAB生成的,我无法在python matplotlib中完成。 谢谢。您可以尝试以下方法来定位椭圆:选择一个x坐标,并计算椭圆的高度,以将提供的函数列表包含在该坐标处 import matplotlib.pyplot as plt import numpy as np from matplotlib.patches import Ellipse x = np.linspace(1,10,
谢谢。您可以尝试以下方法来定位椭圆:选择一个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()
您可以尝试以下方法来定位椭圆:选择一个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()
这是您的解决方案:这是您的解决方案: