Matplotlib: Multiple legends for contour plot for

2019-06-03 06:42发布

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');

1条回答
叼着烟拽天下
2楼-- · 2019-06-03 07:33

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:

enter image description here

Perhaps something like plt.legend(CS2.legend_elements()[0], CS2.legend_elements()[1]), can also be useful for you.

查看更多
登录 后发表回答