我需要在同一页面上制作多个变量的多个等高线图。我可以用 MATLAB 来做到这一点(参见下面的 MATLAB 代码)。我无法让 matplotlib 显示多个图例。任何帮助将非常感激。
Python代码:
import numpy as np
from matplotlib import cm as cm
from matplotlib import pyplot as plt
delta = 0.25
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = X*np.exp(-X**2-Y**2)
Z2 = Y*np.exp(-X**2-Y**2)
plt.figure()
CS = plt.contour(X, Y, Z1, colors='k')
plt.clabel(CS, inline=1, fontsize=10)
CS = plt.contour(X, Y, Z2, colors='r')
plt.clabel(CS, inline=1, fontsize=10)
plt.legend(['case 1', 'case 2'])
plt.show()
MATLAB代码:
[X,Y] = meshgrid(-2:.2:2,-2:.2:3);
Z1 = X.*exp(-X.^2-Y.^2);
Z2 = Y.*exp(-X.^2-Y.^2);
[C,h] = contour(X,Y,Z1, 'color', 'k');
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
hold on
[C,h] = contour(X,Y,Z2, 'color', 'r');
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
fn = {'case 1', 'case 2'};
legend(fn,'Location','NorthWest');