[Tutor] Issues Inserting Graphical Overlay Using Matplotlib Patches
Mark Lawrence
breamoreboy at gmail.com
Mon Sep 28 12:50:44 EDT 2020
On 28/09/2020 17:24, Stephen Malcolm wrote:
> 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
graph.scatter(data[:,0], data[:,1]) as I can't at a quick glance see
anything else.
>
> Can someone please tell me where I'm going wrong?
>
> Thank you in advance...
>
> S
>
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list