25

How do I add a small filled circle or point to a countour plot in matplotlib?

1 Answer 1

38

Here is an example, using pylab.Circle:

import numpy as np
import matplotlib.pyplot as plt

e = np.e
X, Y = np.meshgrid(np.linspace(0, 5, 100), np.linspace(0, 5, 100))
F = X ** Y
G = Y ** X

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
circ = plt.Circle((e, e), radius=0.07, color='g')
plt.contour(X, Y, (F - G), [0])
ax.add_patch(circ)
plt.show()

enter image description here

And here is another example (though not a contour plot) from the docs.

Or, you could just use plot:

import numpy as np
import matplotlib.pyplot as plt

e = np.e
X, Y = np.meshgrid(np.linspace(0, 5, 100), np.linspace(0, 5, 100))
F = X ** Y
G = Y ** X

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
plt.contour(X, Y, (F - G), [0])
plt.plot([e], [e], 'g.', markersize=20.0)
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Let's take it line-by-line. What's the first line that doesn't make sense?
F = X ** Y G = Y ** X This is the one
X and Y are NumPy arrays of shape (100, 100). ** is the exponentiation operator. Arithmetic operations on NumPy arrays are performed element-wise. So X ** Y is the exponentiation of X to the Yth power, done for each element in X with the corresponding element in Y. Try it out in a Python interpreter, perhaps with smaller arrays for X and Y so the result is easier to see.
The plot is showing the roots of the equation x^y = y^x. These is the obvious solution, the straight line x = y. But then there is also that curved line, shown above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.