画两条影响无关变量x
和y
空调上的功能f(x)
和g(y)
之间的关系可以通过在f(x)-g(y) = 0
绘制轮廓来进行。
import numpy as np
import matplotlib.pyplot as plt
f = lambda x: x**2
g = lambda y: np.sqrt(y)
x = np.linspace(0,2,101)
y = np.linspace(0,10,101)
fig, (ax, ax2, ax3) = plt.subplots(ncols=3, figsize=(10,4))
ax.plot(x, f(x))
ax.plot([1.5],[f(1.5)], marker="o")
ax2.plot(y, g(y))
ax2.plot([f(1.5)**2],[f(1.5)], marker="o")
#create a meshgrid from the x and y array
X,Y = np.meshgrid(x,y)
#plot the contour f(X)-g(Y) = 0
c = ax3.contour(X,Y, f(X)-g(Y), 0)
ax3.plot([1.5],[f(1.5)**2], marker="o")
ax.set_ylim([0,4])
ax2.set_ylim([0,4])
ax.set_xlabel("x")
ax.set_ylabel("f(x)")
ax2.set_xlabel("y")
ax2.set_ylabel("g(y)")
ax3.set_xlabel("x")
ax3.set_ylabel("y")
ax3.legend(handles=[c.collections[0]], labels=["f(x) = g(y)"])
plt.tight_layout()
plt.show()