4

I'm using the following code to plot a circle of unit radius centered at the origin:

fig =plt.figure(figsize=(10,7))
x = np.linspace(-5.0, 5.0, 100)
y = np.linspace(-5.0, 5.0, 100)
X, Y = np.meshgrid(x,y)
F = X**2 + Y**2 - 1
plt.contour(X,Y,F,[0])
plt.show()

I now want to shade the area inside the circle with the color blue. How could this be done?

Also, how would I add the x and y axes to this plot and label these axes?

tmdavison
  • 64,360
  • 12
  • 187
  • 165
maths54321
  • 141
  • 2
  • 1
    `plt.contourf(X, Y, F, [-100, 0], cmap='Blues'); plt.xlabel('x'); plt.ylabel('y')` – JohanC Jul 08 '21 at 15:19
  • 2
    An alternative method would be to add a Circle patch: https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Circle.html#matplotlib.patches.Circle – tmdavison Jul 08 '21 at 15:23

1 Answers1

1

To shade a contour area, you can use plt.contourf(). You need to provide at least two levels to be colored in between, e.g. using a very small value together with zero. The function accepts an explicit list of colors (or can work with a colormap).

ax.spines['left'].set_position('center') can set the y-axis at the zero of x, instead of the default placement at the border. ax.set_ylabel('y', loc='top') sets the y-label at the top (default is in the center).

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(10, 7))
x = np.linspace(-5.0, 5.0, 100)
y = np.linspace(-5.0, 5.0, 100)
X, Y = np.meshgrid(x, y)
F = X ** 2 + Y ** 2 - 1
plt.contourf(X, Y, F, [-100, 0], colors=['dodgerblue'])
ax = plt.gca()
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.set_xlabel('x', loc='right')
ax.set_ylabel('y', loc='top')
plt.show()

filled contour plot

Note that Python's symbolic math package sympy does something similar without the need to create a grid.

from sympy import plot_implicit
from sympy.abc import X, Y

plot1 = plot_implicit(X ** 2 + Y ** 2 < 1)

sympy plot_implicit circle

To make changes to the sympy plot, you can either access the ax via plot1._backend.ax[0], or use matplotlib with plt.gca() and plt.gcf() as follows:

from sympy import plot_implicit
from sympy.abc import X, Y

plot1 = plot_implicit(X ** 2 + Y ** 2 < 1)

import matplotlib.pyplot as plt

ax = plt.gca()
ax.set_xlabel('new x label', ha='right')
ax.set_ylabel('new y label', ha='right')
ax.set_aspect('equal') # use same transformation for x and y, so circles aren't deformed
fig = plt.gcf()
fig.set_size_inches(8, 8)
fig.savefig('test_circle.png', dpi=100)
JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Thank you for this. If I want to use the sympy package as you did, how would I change the x and y labels to something else (e.g "time" and "speed")? – maths54321 Jul 17 '21 at 13:27
  • 1
    `plot1._backend.ax[0].set_xlabel('new x label')` and `plot1._backend.ax[0].set_ylabel('new y label')` should work – JohanC Jul 20 '21 at 10:36
  • Thanks again. Just two more things - what code would I type to save the sympy version of the plot as a jpeg file; I know how this is done with matplotlib, but not sympy? And also, how would I change the size of the image of the sympy plot (e.g in matplotlib fig =plt.figure(figsize=(5,5)) could be used)? – maths54321 Jul 25 '21 at 13:18
  • [resizing sympy plot](https://stackoverflow.com/questions/33174301/changing-figure-size-in-sympy-mpmath-plot) via `import matplotlib.pyplot as plt; plt.rcParams['figure.figsize'] = 10, 3`. [Saving sympy plots](https://stackoverflow.com/questions/32151669/saving-python-sympy-figures-with-a-specific-resolution-pixel-density) via `plt.savefig()` – JohanC Jul 25 '21 at 16:00