How to make 3 circles in python
Question:
So I was given an assignment in which I had to make a graph with 3 circles.
I tried to make it by using ‘make_circles’ from sklearn.datsets. I used the code
from sklearn.datasets import make_circles
X_3 , Y_3 = make_circles(n_samples = 5609 , noise = 0.1 , factor = 0, random_state = 42)
plt.scatter(X_3[:, 0], X_3[:, 1])
plt.show()
It is resulting in only 2 circles. I want to insert another circle in the same graph. What to do? Kindly help as this assignment is very important for my grades.
Answers:
I’d strongly recommend you try to do your assignments yourself, as that will genuinely help you in the long run. But that’s not what this site is for, so here goes:
You don’t necessarily need Scikit-Learn to draw circles in a Matplotlib figure; Matplotlib supports drawing circles (and many more shapes) through so-called patches.
A circle at (4, 6) with a radius of 2 can be instantiated as follows:
import matplotlib.pyplot as plt
circle = plt.Circle((4, 6), 2)
This Circle
object can be added to a figure using Axes.add_patch
, for example (plt.gca()
just grabs the current relevant Axes
instance):
plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 10)
plt.gca().add_patch(circle)
plt.show()
This will give us our circle. You may notice that the circle is not entirely… circular. As the documentation states, it’s a ‘true circle’, meaning that it adheres to the scaling of the figure. You could manually fix this using an Ellipse
instead of a Circle
. However, that and how disable filling, linewidth, colours, etc., I will leave as an assignment to the reader.
So I was given an assignment in which I had to make a graph with 3 circles.
I tried to make it by using ‘make_circles’ from sklearn.datsets. I used the code
from sklearn.datasets import make_circles
X_3 , Y_3 = make_circles(n_samples = 5609 , noise = 0.1 , factor = 0, random_state = 42)
plt.scatter(X_3[:, 0], X_3[:, 1])
plt.show()
It is resulting in only 2 circles. I want to insert another circle in the same graph. What to do? Kindly help as this assignment is very important for my grades.
I’d strongly recommend you try to do your assignments yourself, as that will genuinely help you in the long run. But that’s not what this site is for, so here goes:
You don’t necessarily need Scikit-Learn to draw circles in a Matplotlib figure; Matplotlib supports drawing circles (and many more shapes) through so-called patches.
A circle at (4, 6) with a radius of 2 can be instantiated as follows:
import matplotlib.pyplot as plt
circle = plt.Circle((4, 6), 2)
This Circle
object can be added to a figure using Axes.add_patch
, for example (plt.gca()
just grabs the current relevant Axes
instance):
plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 10)
plt.gca().add_patch(circle)
plt.show()
This will give us our circle. You may notice that the circle is not entirely… circular. As the documentation states, it’s a ‘true circle’, meaning that it adheres to the scaling of the figure. You could manually fix this using an Ellipse
instead of a Circle
. However, that and how disable filling, linewidth, colours, etc., I will leave as an assignment to the reader.