Sam Macharia
Sam Macharia

Reputation: 1241

How to delete a section of matplotlib axis

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.

Upvotes: 4

Views: 746

Answers (1)

My Work
My Work

Reputation: 2508

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

Upvotes: 1

Related Questions