ardms
ardms

Reputation: 170

matplotlib separate ticks from spine

Is there a way to define a distance between the ticks and the spine on a line plot? I have managed to create the following (yticks are separated from the spine and the grid lines)

enter image description here

With the following code:

params_1 = {
          'axes.spines.top': False,
          'axes.spines.left': False,
          'axes.spines.right': False,
          'ytick.major.size': 10,
         }
plt.rcParams.update(params_1)

fig = plt.figure(figsize=(13,6.5))
ax = fig.add_subplot(111, facecolor='w')
ax.set_ylim([5,15])
ax.set_xticks([5,10,15,20,25])
ax.yaxis.grid()
ax_xgrid = ax.xaxis.grid(linestyle=':', linewidth=1.5)
[i.set_marker('o') for i in ax.yaxis.get_ticklines()]
[i.set_markeredgecolor('w') for i in ax.yaxis.get_ticklines()]
[i.set_markeredgewidth(4) for i in ax.yaxis.get_ticklines()]

This looks is exactly what I would like but if is save the figure with transparency then I see the white circles around the yticks. Any ideas how to solve this?

Thanks,

Upvotes: 0

Views: 813

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40727

If I understand correctly what you are asking, this can be easily done using the tick_params() helper function.

fig, ax = plt.subplots()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.tick_params(axis='y', color='none', pad=50)
ax.grid()
plt.show()

enter image description here

EDIT I did not understand what you were trying to do. What you want to increase the distance between the left axis and the main part of the plot. To do so, use the Spine.set_position() function. The following should work:

fig, ax = plt.subplots()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['left'].set_position(('outward', 20))
[i.set_marker('o') for i in ax.yaxis.get_ticklines()]
ax.grid()
plt.show()

enter image description here

Upvotes: 2

Related Questions