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

查看:37
本文介绍了隐藏matplotlib中的原点(x和y轴上为0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码,我想隐藏图形中的原点(x 轴上的零和 y 轴上的零).我已经尝试了我见过的所有可能性,但是当我获得标签列表时,它们返回空,或者当范围为时,它们仅返回几个值(-2、-1、0、1、2、3)-10 - 10.

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.

还可以将刻度线本身的字体更改为Computer Modern 10(LaTeX字体吗?)

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

推荐答案

可以使用 FuncFormatter 0 隐藏在轴上.此格式化程序的函数只需检查标签是否为0,并在这种情况下返回一个空字符串即可.

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()

这篇关于隐藏matplotlib中的原点(x和y轴上为0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆