Skip to content

Instantly share code, notes, and snippets.

@chrisraff
Last active May 19, 2020 02:20
Show Gist options
  • Save chrisraff/fd90b1b06e11ba311367082eb4b54b3c to your computer and use it in GitHub Desktop.
Save chrisraff/fd90b1b06e11ba311367082eb4b54b3c to your computer and use it in GitHub Desktop.
Matlab style error plotting for matplotlib pyplot
import matplotlib.pyplot as plt
import numpy as np
# matlab style errorbars
def errorbars(x, y, err, erralpha=0.2, color=None, label=None, ax=plt):
line = ax.plot(x, y, color=color, label=label)
col = line[0].get_color()
plt.fill_between(x, y-err, y+err, alpha=erralpha, color=col, zorder=-1)
# example
if __name__ == '__main__':
data = np.random.random((10, 9))
data_avg = np.mean(data, axis=1)
data_err = np.std(data, axis=1)
# x data is required, as it is with plt.errorbar
errorbars(range(data.shape[0]), data_avg, data_err)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment