[SciPy-Dev] Issues Inserting Graphical Overlay Using Matplotlib Patches

Stephen Malcolm stephen_malcolm at hotmail.com
Thu Oct 1 05:49:20 EDT 2020


Hi Dominik,

Thanks for the feedback. Ok, so I ran your code.
However, I'm getting an error which is highlighted below the code.  Hoping you can shed some light? Appreciate your help on this.

import matplotlib.patches as patches

fig,graph = plt.subplots(figsize=(6, 6))
# just a single subplots call at the beginning as you want it all on a single image
plt.xlabel('V1')
plt.ylabel('V2')
plt.title('Visualization of raw data');
plt.scatter(data.iloc[:, 0], data.iloc[:, 1])
plt.scatter(mean[0],mean[1])
# remove plt.figure(figsize=(6, 6)) here
graph.add_patch(ellipse)
plt.show()

**
Error is:

NameError                                 Traceback (most recent call last)
<ipython-input-4-bd49ad8e0a12> in <module>()
      3 import matplotlib.patches as patches
      4
----> 5 fig,graph = plt.subplots(figsize = (6, 6))
      6 # just a single subplots call at the beginning as you want it all on a single image
      7 plt.xlabel('V1')

NameError: name 'plt' is not defined




________________________________
From: SciPy-Dev <scipy-dev-bounces+stephen_malcolm=hotmail.com at python.org> on behalf of Dominik Stańczak <stanczakdominik at gmail.com>
Sent: 01 October 2020 04:04
To: SciPy Developers List <scipy-dev at python.org>
Subject: Re: [SciPy-Dev] Issues Inserting Graphical Overlay Using Matplotlib Patches

Hey,

Subplots already includes a call to figure, which creates a new active graph as you put it. This is the empty one that pops out. I'd do this:

fig,graph = plt.subplots(figsize=(6, 6))
# just a single subplots call at the beginning as you want it all on a single image
plt.xlabel('V1')
plt.ylabel('V2')
plt.title('Visualization of raw data');
plt.scatter(data.iloc[:, 0], data.iloc[:, 1])
plt.scatter(mean[0],mean[1])
# remove plt.figure(figsize=(6, 6)) here
graph.add_patch(ellipse)
plt.show()

I expect that to work, or at least to head in the right direction :)

Cheers,
Dominik


On Wed, Sep 30, 2020, 19:55 Stephen Malcolm <stephen_malcolm at hotmail.com<mailto:stephen_malcolm at hotmail.com>> wrote:
Hello All,


I'm having some trouble adding a graphical overlay i.e. an ellipse onto my plot.
I wish to do this, as I need to explain/ portray the mean, standard deviation and outliers. And hence evaluate the suitability of the dataset.


Could you please let me know what code I'm missing/ or need to add, in order to insert this ellipse?


I have no trouble plotting the data points and the mean using this code, however, the ellipse (width and height/ standard deviation) doesn't appear.

I have no errors, instead, I'm getting a separate graph (without data points or ellipse) below the plotted one.

Please find my code below:

#pandas used to read dataset and return the data
#numpy and matplotlib to represent and visualize the data
#sklearn to implement kmeans algorithm

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

#import the data
data = pd.read_csv('banknotes.csv')

#extract values
x=data['V1']
y=data['V2']

#print range to determine normalization
print ("X_max : ",x.max())
print ("X_min : ",x.min())
print ("Y_max : ",y.max())
print ("Y_min : ",y.min())


#normalize values
mean_x=x.mean()
mean_y=y.mean()
max_x=x.max()
max_y=y.max()
min_x=x.min()
min_y=y.min()

for i in range(0,x.size):
    x[i] = (x[i] - mean_x) / (max_x - min_x)

for i in range(0,y.size):
    y[i] = (y[i] - mean_y) / (max_y - min_y)

#statistical analyis using mean and standard deviation

import matplotlib.patches as patches

mean = np.mean(data, 0)
std_dev = np.std(data, 0)

ellipse = patches.Ellipse([mean[0], mean [1]], std_dev[0]*2, std_dev[1]*2, alpha=0.25)

plt.xlabel('V1')
plt.ylabel('V2')
plt.title('Visualization of raw data');
plt.scatter(data.iloc[:, 0], data.iloc[:, 1])
plt.scatter(mean[0],mean[1])
plt.figure(figsize=(6, 6))

fig,graph = plt.subplots()

graph.add_patch(ellipse)

Kind Regards,
Stephen

_______________________________________________
SciPy-Dev mailing list
SciPy-Dev at python.org<mailto:SciPy-Dev at python.org>
https://mail.python.org/mailman/listinfo/scipy-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20201001/04c98baf/attachment.html>


More information about the SciPy-Dev mailing list