我正在研究一些代码,我想从图中隐藏原点(x轴上的零点和y轴上的零点)。 我已经尝试了所有可能性,但是当我得到标签列表时,它们返回空白,或者当范围为2时,它们只返回几个值(-2,-1,0,1,2,3)。 -10 - 10。
另外,有没有办法将刻度本身的字体改为Computer Modern 10(LaTeX字体?)
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?)
最满意答案
可以使用FuncFormatter隐藏轴上的0 。 此格式化程序的函数只是检查标签是否为0并在这种情况下返回一个空字符串。
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()
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()更多推荐
发布评论