【发布时间】:2017-06-21 03:16:12
【问题描述】:
我正在编写一些代码,我想从图表中隐藏原点(x 轴上的零,y 轴上的零)。我已经尝试了我见过的所有可能性,但是当我得到标签列表时,它们返回空,或者当范围为时它们只返回几个值(-2、-1、0、1、2、3) -10 - 10。
另外,有没有办法将刻度本身的字体更改为 Computer Modern 10(LaTeX 字体?)
【问题讨论】:
标签: python-2.7 matplotlib
我正在编写一些代码,我想从图表中隐藏原点(x 轴上的零,y 轴上的零)。我已经尝试了我见过的所有可能性,但是当我得到标签列表时,它们返回空,或者当范围为时它们只返回几个值(-2、-1、0、1、2、3) -10 - 10。
另外,有没有办法将刻度本身的字体更改为 Computer Modern 10(LaTeX 字体?)
【问题讨论】:
标签: python-2.7 matplotlib
可以使用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()
【讨论】: