How to Draw a Circle in Python Plot

Hello coders!! In this article, we will larn how to brand a circle using matplotlib in Python. A circle is a figure of round shape with no corners. There are various ways in which one tin plot a circle in matplotlib. Let us talk over them in detail.

Method i: matplotlib.patches.Circle():

  • SYNTAX:
    • class matplotlib.patches.Circumvolve(xy,radius=r,**kwargs)
  • PARAMETERS:
    • xy: (x,y) center of the circumvolve
    • r: radius of the circumvolve
  • RESULT: a circle of radius r with middle at (x,y)
import matplotlib.pyplot as plt   figure, axes = plt.subplots()  cc = plt.Circle(( 0.5 , 0.5 ), 0.4 )   axes.set_aspect( 1 )  axes.add_artist( cc )  plt.title( 'Colored Circle' )  plt.show()                

Output & Caption:

matplotlib.patches.Circle
Output

Here, we have used the circle() method of the matplotlib module to draw the circle. We adjusted the ratio of y unit to x unit using the set_aspect() method. We gear up the radius of the circumvolve equally 0.4 and made the coordinate (0.5,0.5) every bit the center of the circle.

Method ii: Using the equation of circle:

The equation of circle is:

  • ten = r cos θ
  • y = r sin θ

r: radius of the circumvolve

This equation tin can be used to draw the circle using matplotlib.

import numpy as np  import matplotlib.pyplot every bit plt   angle = np.linspace( 0 , two * np.pi , 150 )   radius = 0.iv  x = radius * np.cos( angle )  y = radius * np.sin( bending )   figure, axes = plt.subplots( 1 )   axes.plot( x, y )  axes.set_aspect( ane )   plt.title( 'Parametric Equation Circle' )  plt.show()                

Output & Explanation:

equation of circle
Output

In this case, nosotros used the parametric equation of the circumvolve to plot the figure using matplotlib. For this instance, we took the radius of the circle as 0.iv and set the aspect ratio as ane.

Method 3: Scatter Plot to plot a circle:

A besprinkle plot is a graphical representation that makes apply of dots to stand for values of the two numeric values.  Each dot on the xy axis indicates value for an individual data point.

  • SYNTAX:
    • matplotlib.pyplot.scatter(x_axis_data, y_axis_data, southward=None, c=None, marker=None, cmap=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None)
  • PARAMETERS:
    • x_axis_data-  x-centrality data
    • y_axis_data- y-centrality data
    • s- mark size
    • c- color of sequence of colors for markers
    • mark- mark style
    • cmap- cmap name
    • linewidths- width of marker border
    • edgecolor- marker border-color
    • blastoff- blending value
import matplotlib.pyplot as plt   plt.scatter( 0 , 0 , s = 7000 )   plt.xlim( -0.85 , 0.85 )  plt.ylim( -0.95 , 0.95 )   plt.title( "Scatter plot of points Circumvolve" )  plt.show()                

Output & Explanation:

Scatter Plot
Output

Here, we have used the scatter plot to describe the circumvolve. The xlim() and the ylim() methods are used to prepare the 10 limits and the y limits of the axes respectively. We've set the marking size as 7000 and got the circle as the output.

Method 4: Matplotlib hollow circle:

import matplotlib.pyplot as plt   plt.scatter( 0 , 0 , s=10000 ,  facecolors='none', edgecolors='bluish' )   plt.xlim( -0.5 , 0.5 )  plt.ylim( -0.5 , 0.five )   plt.evidence()                

Output & Explanation:

Matplotlib hollow circle
Output

To make the circle hollow, we take set the facecolor parameter as none, and so that the circle is hollow. To differentiate the circle from the plane we have set the edgecolor equally blueish for better visualization.

Method v: Matplotlib draw circle on image:

import matplotlib.pyplot as plt import matplotlib.patches equally patches import matplotlib.cbook equally cb   with cb.get_sample_data('C:\\Users\\Prachee\\Desktop\\cds\\img1.jpg') as image_file:     image = plt.imread(image_file)  fig, ax = plt.subplots() im = ax.imshow(prototype) patch = patches.Circumvolve((100, 100), radius=80, transform=ax.transData) im.set_clip_path(patch)  ax.axis('off') plt.bear witness()                

Output & Explanation:

draw circle on image
Output

In this case, we offset loaded our information and then used the axes.imshow() method. This method is used to display data equally an prototype. We then ready the radius and the center of the circumvolve. And then using the set_clip_path() method we set the artist's clip-path.

Method 6: Matplotlib transparent circumvolve:

import matplotlib.pyplot every bit plt   figure, axes = plt.subplots()  cc = plt.Circumvolve(( 0.five , 0.5 ), 0.iv , alpha=0.1)   axes.set_aspect( i )  axes.add_artist( cc )  plt.title( 'Colored Circle' )  plt.testify()                

Output & Explanation:

Matplotlib transparent circle
Output

To make the circle transparent we changed the value of the blastoff parameter which is used to control the transparency of our effigy.

Conclusion:

With this, we come up to an end with this article. These are the various ways in which one can plot a circle using matplotlib in Python.

However, if you accept whatever doubts or questions, do let me know in the annotate section below. I will attempt to aid yous every bit shortly every bit possible.

Happy Pythoning!

wellsbrieforetwor.blogspot.com

Source: https://www.pythonpool.com/matplotlib-circle/

0 Response to "How to Draw a Circle in Python Plot"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel