1

I have two data sets, which I'd like to scatter plot next to each other with error bars. Below is my code to plot one data set with error bars. And also the code to generate the second data set. I'd like the points and errors for each data for each value to be adjacent.

I'd also like to remove the line connecting the dots.

import random
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as ss

data = []
n = 100
m = 10

for i in xrange(m):
    d = []
    for j in xrange(n):
        d.append(random.random())
    data.append(d)

mean_data = []
std_data = []

for i in xrange(m):
    mean = np.mean(data[i])
    mean_data.append(mean)
    std = np.std(data[i])
    std_data.append(std)



df_data = [n] * m
plt.errorbar(range(m), mean_data, yerr=ss.t.ppf(0.95, df_data)*std_data)
plt.scatter(range(m), mean_data)
plt.show()


new_data = []


for i in xrange(m):
    d = []
    for j in xrange(n):
        d.append(random.random())
    new_data.append(d)


mean_new_data = []
std_new_data = []

for i in xrange(m):
    mean = np.mean(new_data[i])
    mean_new_data.append(mean)
    std = np.std(new_data[i])
    std_new_data.append(std)



df_new_data = [n] * m

enter image description here

1 Answer 1

2

To remove the line in the scatter plot use the fmt argument in plt.errorbar(). The plt.scatter() call is then no longer needed. To plot a second set of data, simply call plt.errorbar() a second time, with the new data.

If you don't want the datasets to overlap, you can add some small random scatter in x to the new dataset. You can do this in two ways, add a single scatter float with

random.uniform(-x_scatter, x_scatter)

which will move all the points as one:

or generate a random scatter float for each point with

x_scatter = np.random.uniform(-.5, .5, m)

which generates something like

enter image description here

To plot both datasets (using the second method), you can use:

plt.errorbar(
    range(m), mean_data, yerr=ss.t.ppf(0.95, df_data)*std_data, fmt='o',
    label="Data")
# Add some some random scatter in x
x_scatter = np.random.uniform(-.5, .5, m)
plt.errorbar(
    np.arange(m) + x_scatter, mean_new_data,
    yerr=ss.t.ppf(0.95, df_new_data)*std_new_data, fmt='o', label="New data")
plt.legend()
plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

But my legend looks different from yours. Why does my legend appear as follows (with your code)? I get two points with error bars next to data and new data in my legend. Can't seem to past image here...?
(You can't post images in comments, try pasteboard.co) You probably are using Python 2.7, mine is 3.6. To only show a single point in the legend, use: plt.legend(numpoints=1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.