Subhasish
Subhasish

Reputation: 7

How to draw a circle in python?

I'm trying to draw a circle in Python using this code:

  import matplotlib.pyplot as plt
  import matplotlib.patches as patches
  def Circle(radius):
       circle=patches.Circle((0,0),radius,facecolor='red',/
              edgecolor='blue',linestyle='dotted',linewidth='2.2')
       plt.gca().add_patch(circle)
       plt.plot(circle)
       plt.axis('axis')
       plt.title('Circle')
       plt.grid()
       plt.show()
 def main():
       radius=float(input('Enter the radius:'))
       Circle(radius)
 main()

The error which is appearing in console is the following:

TypeError: float() argument must be a string or a number, not 'Circle'

What is the error in above mentioned code?

Upvotes: 0

Views: 7141

Answers (2)

Juan C
Juan C

Reputation: 6132

A simplified version of what you're doing:

def circle():
   radius = float(input('Enter the radius:'))
   circle=plt.Circle((0,0),radius,facecolor='red',
             edgecolor='blue',linestyle='dotted',linewidth='2.2')
   plt.gca().add_patch(circle)
   plt.plot(circle)
   plt.axis('axis')
   plt.title('Circle')
   plt.grid()
   plt.show()

circle()

Main differences:

  • Just import plt
  • Lowercase your function (by convention we use uppercase for classes, lowercase for functions).
  • Add the input inside the function.
  • Remove the radius argument

Upvotes: 0

Shmn
Shmn

Reputation: 703

import matplotlib.pyplot as plt
#import matplotlib.patches as patches
def Circle(radius):
    circle=plt.Circle((0,0),radius,facecolor='red', edgecolor='blue',linestyle='dotted',linewidth='2.2')
    plt.gca().add_patch(circle)
    plt.plot()
    #plt.axis('axis')
    plt.title('Circle')
    plt.grid()
    plt.show()
def main():
    radius=float(input('Enter the radius:'))
    Circle(float(radius))

main()

Upvotes: 1

Related Questions