[Tutor] Issues Inserting Graphical Overlay Using Matplotlib Patches

Stephen Malcolm stephen_malcolm at hotmail.com
Mon Sep 28 12:24:08 EDT 2020


Dear all,

I'm having some trouble inserting a graphical overlay in python.
I've highlighted the code (in red) I'm using for this specific operation i.e. the stats and overlay part (and where I'm getting the error)::

#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())

# Import the sklearn function
from sklearn.preprocessing import StandardScaler

# standardize the data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
X_scaled

#statistical analyis using mean and standard deviation

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

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)

graph.scatter(data[:,0])data[:,1])
graph.scatter(mean[0], mean[1])
graph.add_patch(ellipse)

Error states:

ile "<ipython-input-21-1a9b341de4cf>", line 16
    graph.scatter(data[:,0])data[:,1])
                               ^
SyntaxError: invalid syntax

Can someone please tell me where I'm going wrong?

Thank you in advance...

S






More information about the Tutor mailing list