Shikha Singh
Shikha Singh

Reputation: 41

How to add multiple labels to contour lines using clabels

I am trying to plot a contour map in matplotlib. I want the labels to be printed at a certain interval in the plot for more clarity, but I am unable to do so. How can I do that?

The following plot has multiple labels per contour lines:

MATLAB

However, in , I get only one label per contour line.

matplotlib

How can I get multiple labels in matplotlib?

Upvotes: 2

Views: 3627

Answers (2)

swatchai
swatchai

Reputation: 18822

In addition to the answer provided by @krm, you can use manual option to set contour labels' location. The relevant code is:-

plt.figure()
CS = plt.contour(X, Y, Z)
manual_locations = [(-1, -1.4), (-0.62, -0.7), (-2, 0.5), (1.7, 1.2), (2.0, 1.4)]
plt.clabel(CS, inline=1, fontsize=10, manual=manual_locations)

Upvotes: 1

krm
krm

Reputation: 937

You can just apply the label again:

fig, ax = plt.subplots()
CS = ax.contour(X, Y, Z)
ax.clabel(CS, [-1, 0.5,], inline=1, fontsize=10)
ax.clabel(CS, [0.5,], inline=1, fontsize=10)

This will give you two labels on the contour with te level 0.5. I don't quite see a way to control their position, but as far as I can see, they seem to be spaced evenly automatically. Here is the output wth the data generated as per this link.

Two contour levels

Upvotes: 2

Related Questions