Skip to content Skip to sidebar Skip to footer

Matplotlib Scatterplot Error Bars Two Data Sets

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 se

Solution 1:

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

Post a Comment for "Matplotlib Scatterplot Error Bars Two Data Sets"