在matplotlib中隐藏原点(x和y轴上的0)(Hide the origin (0 on x and y axis) in matplotlib)

编程入门 行业动态 更新时间:2024-10-27 11:22:04
在matplotlib中隐藏原点(x和y轴上的0)(Hide the origin (0 on x and y axis) in matplotlib)

我正在研究一些代码,我想从图中隐藏原点(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()

enter image description here

更多推荐

本文发布于:2023-07-24 17:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1248857.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:原点   matplotlib   Hide   axis   origin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!