0

I'm currently doing one of my 1st Coursera courses on Data Science (K-Means). I'm doing fine with almost everything but got stuck on a simple task. Plotting two ellipses on my "clusters" to highlight the distance of the "centroids" to it's STDs.

Must be something foolish but I browsed Google and the course itself and got no luck.

The code (pretty amateur I know) and plot it generates are bellow.

plt.figure(figsize=(8, 8))
plt.title('Cluster Segregtion')
plt.scatter(c0['V1n'], c0['V2n'], color = 'blue', s = 30 , alpha = 0.5)
plt.scatter(c1['V1n'], c1['V2n'], color = 'red', s = 30 , alpha = 0.5)
plt.scatter(clusters[:,0], clusters[:,1], color = 'orange', s = 40000, alpha = 0.3)

Where you see circles should be ellipses

Can someone give me a hand with a simple ellipse code?

Best regards FC

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • [Creating a Confidence Ellipses in a sccatterplot using matplotlib](https://stackoverflow.com/questions/20126061/creating-a-confidence-ellipses-in-a-sccatterplot-using-matplotlib) and [Plot a confidence ellipse](https://matplotlib.org/devdocs/gallery/statistics/confidence_ellipse.html) – JohanC Jun 25 '20 at 07:19

1 Answers1

0

You can use the patch module of matplotlib. For example (with some made up numbers):

from matplotlib.patches import Ellipse
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1)
ax.scatter([2,4,6,3,4,5,4,], [1,8,9,3,9,7,2,]) 
patch = Ellipse((4,5), width=9, height=3, angle=75, alpha=0.4, ls='solid', ec='g', fc = 'r', lw='2.')
ax.add_patch(patch)

You may want to find the coordinates and angle from the data itself, though, but this is a separate question. You can read matplotlib documentation about patches.    

BossaNova
  • 1,509
  • 1
  • 13
  • 17