I need to make multiple contours plots of several variables on the same page. I can do this with MATLAB (see below for MATLAB code). I cannot get matplotlib to show multiple legends. Any help would be much appreciated.
Python code:
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 code:
[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');
It would help if you showed your desired output from Matlab. For example, do you really want multiple legends? Or do you actually mean 1 legend with muliple items?
Since contour plots (can) have a different style for each level, its not obvious how you would want to plot that in a legend. But to get you started, you can access each individual line by examining the CS.collections array.
So for example:
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)
fig, ax = plt.subplots()
CS1 = ax.contour(X, Y, Z1, colors='k')
ax.clabel(CS1, inline=1, fontsize=10)
CS2 = ax.contour(X, Y, Z2, colors='r')
ax.clabel(CS2, inline=1, fontsize=10)
lines = [ CS1.collections[0], CS1.collections[-1], CS2.collections[0], CS2.collections[-1]]
labels = ['CS1_neg','CS1_pos','CS2_neg','CS2_pos']
plt.legend(lines, labels)
Results in:

Perhaps something like plt.legend(CS2.legend_elements()[0], CS2.legend_elements()[1]), can also be useful for you.
It has been some time since this question was asked. I had the same question (probably, as the question of the poster is not entirely clear). The solution to my own question was not mentioned anywhere I could find, and I thought it may be helpful here as well.
My own question was how to create one legend with two entries, one for each set of contourlines in a single plot. I adapted original posters code as such:
import numpy as np
from matplotlib.lines import Line2D
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=True)
CS = plt.contour(X, Y, Z2, colors='r')
plt.clabel(CS,inline=True)
proxies = [Line2D([],[],color=c) for c in ['k','r']]
plt.legend(proxies,['case 1', 'case 2'])
plt.show()
to give the following output.
One legend entry per set of contourlines
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With