Place text inside a circle in Matplotlib

MatplotlibPythonData Visualization

To place text inside a circle in matplotlib, we can take the following steps −

  • Create a new figure or activate an existing figure using figure() method.

  • Add a subplot method to the current axis.

  • Create a Circle instance using Circle() class.

  • Add a circle path on the plot.

  • To place the text in the circle, we can use text() method.

  • Scale x and y axes using xlim() and ylim() methods.

  • To display the figure, use show() method.

Example

import matplotlib
from matplotlib import pyplot as plt, patches
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111)
circle = matplotlib.patches.Circle((0, 0), radius=1, color='yellow')
ax.add_patch(circle)
plt.text(-.25, 0, "This is a Circle")
plt.xlim([-4, 4])
plt.ylim([-4, 4])
plt.axis('equal')
plt.show()

Output

raja
Published on 12-May-2021 11:47:14
Advertisements