Bruno
Bruno

Reputation: 1471

Categorical x-axis errorbar plot in matplotlib

I'd like to plot errorbars with categorical X variable. The error bars (upper and lower) are on Y values only.

For example, the code

import numpy as np
import matplotlib.pyplot as plt

x = ["4", "10", "50"]
y = [3, 2, 1]
yerr = np.matrix([[1.5, 1.1, 0.9], [1.3, 1.2, 0.8]])

fig, ax = plt.subplots(1, 1)
ax.errorbar(x, y, yerr=yerr)
plt.show()
plt.close()

gives the following error:

ValueError: In safezip, len(args[0])=3 but len(args[1])=1

Upvotes: 1

Views: 2653

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339560

The error you get has nothing to do with categorical axis.

You just cannot use a matrix. Use a numpy array,

yerr = np.array([[1.5, 1.1, 0.9], [1.3, 1.2, 0.8]])

or simply a list, there is no need to use numpy here,

yerr = [[1.5, 1.1, 0.9], [1.3, 1.2, 0.8]]

Upvotes: 2

Related Questions