4

I want to have a plot like the one shown in this figure: Desired errorbar plot

Here is my Python3 code:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0.1,1.1,step=0.1)
y = np.array([0.03,0.03,0.05,0.05,0.06,0.17,0.44,1.37,4.43,6.89])
err = np.array([0.02,0.03,0.05,0.06,0.07,0.23,3.61,4.70,1.2,0.7])

fig, ax = plt.subplots(figsize=(10,8))

ax.errorbar(x,y,yerr=err)

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_position(('axes',-0.02))
ax.spines['left'].set_position(('axes',-0.02))

ax.set_xticks(np.arange(0.0,1.21,0.2))
ax.set_yticks(np.arange(0,11,2))

ax.set_xlabel('X')
ax.set_ylabel('Y')

plt.show()

From the above code, I get this plot: Errorbar plot with an unwanted axis section

If I use ax.set_ylim([0,10]) instead of ax.set_yticks(np.arange(0,11,2)), I get the following figure having truncated the error bar: Truncated error bar

Any leads would be highly appreciated.

2

1 Answer 1

1

Use ax.spines['left'].set_bounds(0, 10).

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0.1,1.1,step=0.1)
y = np.array([0.03,0.03,0.05,0.05,0.06,0.17,0.44,1.37,4.43,6.89])
err = np.array([0.02,0.03,0.05,0.06,0.07,0.23,3.61,4.70,1.2,0.7])
fig, ax = plt.subplots(figsize=(10,8))

ax.errorbar(x,y,yerr=err)

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_position(('axes',-0.02))
ax.spines['left'].set_bounds(0, 10)

ax.set_xticks(np.arange(0.0,1.21,0.2))
ax.set_yticks(np.arange(0,11,2))

ax.set_xlabel('X')
ax.set_ylabel('Y')

enter image description here

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.