Issues Inserting Graphical Overlay Using Matplotlib Patches

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

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@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@python.org https://mail.python.org/mailman/listinfo/scipy-dev

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@python.org> on behalf of Dominik Stańczak <stanczakdominik@gmail.com> Sent: 01 October 2020 04:04 To: SciPy Developers List <scipy-dev@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@hotmail.com<mailto:stephen_malcolm@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@python.org<mailto:SciPy-Dev@python.org> https://mail.python.org/mailman/listinfo/scipy-dev

import matplotlib.pyplot as plt? On Thu, 1 Oct 2020 at 11.49, Stephen Malcolm <stephen_malcolm@hotmail.com> wrote:
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@python.org> on behalf of Dominik Stańczak < stanczakdominik@gmail.com>
*Sent:* 01 October 2020 04:04
*To:* SciPy Developers List <scipy-dev@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@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@python.org
https://mail.python.org/mailman/listinfo/scipy-dev
_______________________________________________
SciPy-Dev mailing list
SciPy-Dev@python.org

I tried your line of code Andrea. I get the plot, but no ellipse. I think I need the matiplotlib.patches, otherwise it’s not possible to get the overlay/ ellipse Sent from my iPhone On 2020. Oct 1., at 12:43, Andrea Gavana <andrea.gavana@gmail.com> wrote: import matplotlib.pyplot as plt? On Thu, 1 Oct 2020 at 11.49, Stephen Malcolm <stephen_malcolm@hotmail.com<mailto:stephen_malcolm@hotmail.com>> wrote: 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@python.org<mailto:hotmail.com@python.org>> on behalf of Dominik Stańczak <stanczakdominik@gmail.com<mailto:stanczakdominik@gmail.com>> Sent: 01 October 2020 04:04 To: SciPy Developers List <scipy-dev@python.org<mailto:scipy-dev@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@hotmail.com<mailto:stephen_malcolm@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@python.org<mailto:SciPy-Dev@python.org> https://mail.python.org/mailman/listinfo/scipy-dev _______________________________________________ SciPy-Dev mailing list SciPy-Dev@python.org<mailto:SciPy-Dev@python.org> https://mail.python.org/mailman/listinfo/scipy-dev _______________________________________________ SciPy-Dev mailing list SciPy-Dev@python.org https://mail.python.org/mailman/listinfo/scipy-dev

Hello All, For your information, I managed to rectify the code to incorporate my ellipse onto one plot. I used some of Dominick's suggestions, but I also moved the 'fig,graph = plt.subplots(figsize=(6,6))' line just below the ellipse syntax i.e. 'ellipse = patches.Ellipse([mean[0], mean [1]], std_dev[0]*2, std_dev[1]*2, alpha=0.25)'. This had made all the difference. Thanks for all your input. #statistical analysis 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) fig,graph = plt.subplots(figsize=(6,6)) graph.scatter(data.iloc[:, 0], data.iloc[:, 1]) graph.scatter(mean[0],mean[1]) plt.xlabel('V1') plt.ylabel('V2') plt.title('Visualization of raw data'); graph.add_patch(ellipse) ________________________________ From: SciPy-Dev <scipy-dev-bounces+stephen_malcolm=hotmail.com@python.org> on behalf of Andrea Gavana <andrea.gavana@gmail.com> Sent: 01 October 2020 10:42 To: SciPy Developers List <scipy-dev@python.org> Subject: Re: [SciPy-Dev] Issues Inserting Graphical Overlay Using Matplotlib Patches import matplotlib.pyplot as plt? On Thu, 1 Oct 2020 at 11.49, Stephen Malcolm <stephen_malcolm@hotmail.com<mailto:stephen_malcolm@hotmail.com>> wrote: 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@python.org<mailto:hotmail.com@python.org>> on behalf of Dominik Stańczak <stanczakdominik@gmail.com<mailto:stanczakdominik@gmail.com>> Sent: 01 October 2020 04:04 To: SciPy Developers List <scipy-dev@python.org<mailto:scipy-dev@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@hotmail.com<mailto:stephen_malcolm@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@python.org<mailto:SciPy-Dev@python.org> https://mail.python.org/mailman/listinfo/scipy-dev _______________________________________________ SciPy-Dev mailing list SciPy-Dev@python.org<mailto:SciPy-Dev@python.org> https://mail.python.org/mailman/listinfo/scipy-dev
participants (3)
-
Andrea Gavana
-
Dominik Stańczak
-
Stephen Malcolm