theta
theta

Reputation: 25631

Adding label to contour

This code:

import numpy as np
import matplotlib.pyplot as plt

plt.contourf(np.random.random((10,10)), label='my_label')
plt.legend()

produces this warning:

warnings.warn("No labeled objects found.")

Does anyone know how to label contour?
I just want to annotate automatically generated contours inside plot window the easiest possible way.

Upvotes: 4

Views: 3766

Answers (1)

theta
theta

Reputation: 25631

I used annotate() function to label my filled contour:

screenshot

In above code, instead plt.legend() I used:

plt.annotate('my_label', (8, 1), backgroundcolor='w')

So only additional consideration compared to label parameter, is that user needs to know the coordinates where the text will be positioned. Or so I think.

Update: As suggested in the comments, user can choose axes fraction type for xycoords parameter and use relative reference in range [0,1] to set mandatory xy point parameter:

plt.annotate('my_label', (.9, .1), xycoords='axes fraction', backgroundcolor='w')

Upvotes: 2

Related Questions