1

I want to create a graph with different variables on the x axis, and plot two confidence intervals for each variable. I have used plt.errorbar to achieve this. My problem is that I can't find a way to specify labels for the x-axis variables. Currently, the x-axis is just integers. If I change it to strings, then I am unable to plot the two confidence intervals per variable. I use "x = ... +0.2" to get them on the same plot.

plt.errorbar(
    x=range(10,
    y= data1,
    yerr = data1_err,
)

plt.errorbar(
    x=range(10),
    y= data2,
    yerr=data2_err,
)
plt.show()

Could anyone offer some hints or if its not possible with this function, to perhaps recommend another one? I need to plot vertical lines indicating confidence intervals, and they will be over both positive and negative values of y.

2
  • Could you clarify what exactly you want to do? Are you able to provide an example image with the expected output? Commented Jun 16, 2018 at 20:57
  • @Gabriel Something like this: researchgate.net/figure/… I want to have two confidence intervals per variable, but I also want to have words as my x axis variables, rather than numbers Commented Jun 16, 2018 at 21:15

1 Answer 1

3

The following code makes use of matplotlib's plot markers and ticks customization:

import matplotlib.pyplot as plt
import numpy as np

x_ticks = ("Thing 1", "Thing 2", "Other thing", "Yet another thing")

x_1 = np.arange(1, 5)
x_2 = x_1 + 0.1

y_1 = np.random.choice(np.arange(1, 7, 0.1), 4)
y_2 = np.random.choice(np.arange(1, 7, 0.1), 4)

err_1 = np.random.choice(np.arange(0.5, 3, 0.1), 4)
err_2 = np.random.choice(np.arange(0.5, 3, 0.1), 4)

plt.errorbar(x=x_1, y=y_1, yerr=err_1, color="black", capsize=3,
             linestyle="None",
             marker="s", markersize=7, mfc="black", mec="black")

plt.errorbar(x=x_2, y=y_2, yerr=err_2, color="gray", capsize=3,
             linestyle="None",
             marker="s", markersize=7, mfc="gray", mec="gray")

plt.xticks(x_1, x_ticks, rotation=90)

plt.tight_layout()
plt.show()

The output image will look something like this:

Error bars + custom ticks

Sign up to request clarification or add additional context in comments.

Comments

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.