Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Show Mean in Scatterplot

I have a dataset with three columns. The first shows the actual data and the
second shows the mean of the first. The third is a category. I want to show both the data and the
mean in a scatterplot. However, the mean is plotted N times, not only once –
making it difficult to control the visualization. Is there a way to plot
the mean only once?

Here is a toy example where the orange dots (indicating the mean) are plotted 14 times on top of each other.

import matplotlib.pyplot as plt
import pandas as pd
  

df = pd.DataFrame({
    'Numbers': [10,20,30,40,50,60,11,59,12,58,13,57,25,45],
    'Mean':    [35,35,35,35,35,35,35,35,35,35,35,35,35,35],
    'y':       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]    
    })

fig, ax = plt.subplots(1)
ax.scatter(y="y", x= "Numbers", data=df, s = 200, alpha = .5, label = "Mean response")
ax.scatter(y="y", x= "Mean", data=df, s = 200, alpha = .15, label = "Average mean response")
legend = ax.legend(loc="lower right", framealpha = .4)

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just plot one value:

fig, ax = plt.subplots(1)
ax.scatter(y="y", x="Numbers", data=df, s=200,
           alpha=.5, label="Average mean response")

ax.scatter(df['Numbers'].mean(), df['y'].mean(),  # plot one value, not 15
           s=200, alpha=.15,
           label="Average mean response")

legend = ax.legend(loc="lower right", framealpha = .4)

output:

scatter plot with mean

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading