0

I have a simple plot executed using matplotlib. My x and y axes start at 0,0 respectively. A matplotlib plot shows 2 zeros corresponding to the 2 axes. I want only one zero (somewhere in the middle of the start point, if possible). How can this be done?

Here's what I used:

import matplotlib.pyplot as plt

plt.plot([1,2,3], [4,5,6])
plt.xlim([0,5])
plt.ylim([0,10])
plt.show()

Matplotlib Plot

UPDATE:

I used @nostradamus' solution and it got rid of one of the zeros. I want the zero a little centred if possible.

I used:

plt.gca().xaxis.set_major_locator(MaxNLocator(prune='lower')) 
plt.gca().yaxis.get_majorticklabels()[0].set_x(-0.05)

enter image description here

I want the reverse of this. I want the zero on the y axis to move down or the one from x axis left. So tried:

plt.gca().yaxis.set_major_locator(MaxNLocator(prune='lower')) 
plt.gca().xaxis.get_majorticklabels()[0].set_x(-0.05)

It doesn't work. I think the bottom and left boundaries for the zeros are set to ensure they don't go beyond the area.

1 Answer 1

3

The keyword is prune, which allows you to kill the upper, lower or both tick labels. Here is a working examples that gets rid of the 0 of the x-axis:

import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

plt.plot([1,2,3], [4,5,6])
plt.xlim([0,5])
plt.ylim([0,10])
plt.gca().xaxis.set_major_locator(MaxNLocator(prune='lower'))
plt.show()

The second part (to move the remaining zero to the corner) seems to be much more difficult. It seems that you can move single tick labels using

plt.gca().yaxis.get_majorticklabels()[0].set_x(-0.05)

However, I was unable to figure out how to move it below the lower limit of the corresponding axis (i.e. plt.gca().yaxis.get_majorticklabels()[0].set_y(-0.05) is doing nothing).

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

1 Comment

Thanks so much. I didn't know about prune. And yeah it seems to be really difficult editing the graphs created using pyplot

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.