Error bars in 1 direction but direction varies from point to point

Viewed 298

I have a dataset plotted using plt.scatter(x,y) and an array of individual errors yerr and xerr that I want to put on each point as error bars using plt.errorbar(x,y,xerr,yerr, fmt='none').

As some values in xerr and yerr are positive, but some are negative I only want the error bar to go in the +ve or -ve direction according to this but when I use this code, it automatically plots in both directions.

How can I get it to just plot in a single, variable direction based on if the error array value is +ve or -ve?

I can think of a way to do it by creating new arrays to plot positive and negative directions but surely there's a quicker, easier way...

1 Answers

When providing one array for the x-errors, they are interpreted as symmetrical errors. To have different error bars left and right, a tuple of two arrays can be given: one array with maximum(0, -xerr) and one with maximum(0, xerr). Note that these values are supposed to be positive. The y-errors work similarly.

import matplotlib.pyplot as plt
import numpy as np

x, y = np.random.uniform(1, 50, size=(2, 20))
xerr, yerr = np.random.uniform(-4, 4, size=(2, 20))
plt.errorbar(x, y,
             xerr=(np.maximum(0, -xerr), np.maximum(0, xerr)),
             yerr=(np.maximum(0, -yerr), np.maximum(0, yerr)),
             fmt='none', color='navy')
plt.show()

example plot

Here is an example with x and y on a grid:

x = np.tile(np.arange(10, 160, 10), 10)
y = np.repeat(np.arange(10, 110, 10), 15)
xerr, yerr = np.random.uniform(-4, 4, size=(2, len(x)))
plt.errorbar(x, y,
             xerr=(np.maximum(0, -xerr), np.maximum(0, xerr)),
             yerr=(np.maximum(0, -yerr), np.maximum(0, yerr)),
             fmt='none', color='navy')
plt.show()

x and y on grid

PS: The same plot can also be generated via short horizontal and vertical lines, calling plt.hlines() and plt.vlines():

plt.hlines(y, x, x+xerr, color='navy')
plt.vlines(x, y, y+yerr, color='navy')
Related