Axel Persinger
Axel Persinger

Reputation: 331

Hide the origin (0 on x and y axis) in matplotlib

I'm working on some code and I'd like to hide the origin (the zero on the x-axis, and the zero on the y-axis) from the graph. I've tried all possibilities I've seen, but when I get the list of labels they return empty, or they return with only a few values (-2, -1, 0, 1, 2, 3) when the range is -10 - 10.

Also, is there any way to change the font of the ticks themselves to Computer Modern 10 (The LaTeX font?)

Upvotes: 0

Views: 2901

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339220

Hiding the 0 on the axes could be done using a FuncFormatter. The function for this formatter would simply check if the label is 0 and return an empty string in that case.

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np

x = np.linspace(-5,8)
y = np.sin(x)
plt.plot(x,y)

func = lambda x, pos: "" if np.isclose(x,0) else x
plt.gca().xaxis.set_major_formatter(ticker.FuncFormatter(func))
plt.gca().yaxis.set_major_formatter(ticker.FuncFormatter(func))

plt.show()

enter image description here

Upvotes: 3

Related Questions