Python 隐藏轴值但在 matplotlib 中保留轴刻度标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37039685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:43:56  来源:igfitidea点击:

Hide axis values but keep axis tick labels in matplotlib

pythonmatplotlib

提问by Luis Ramon Ramirez Rodriguez

I have this image:

我有这张图片:

plt.plot(sim_1['t'],sim_1['V'],'k')
plt.ylabel('V')
plt.xlabel('t')
plt.show()

enter image description here

在此处输入图片说明

I want to hide the numbers; if I use:

我想隐藏数字;如果我使用:

plt.axis('off')

...I get this image:

...我得到这张图片:

enter image description here

在此处输入图片说明

It also hide the labels, Vand t. How can I keep the labels while hiding the values?

它还隐藏标签,Vt。如何在隐藏值的同时保留标签?

回答by tmdavison

If you use the matplotlib object-oriented approach, this is a simple task using ax.set_xticklabels()and ax.set_yticklabels():

如果您使用 matplotlib面向对象的方法,这是一个使用ax.set_xticklabels()and的简单任务ax.set_yticklabels()

import matplotlib.pyplot as plt

# Create Figure and Axes instances
fig,ax = plt.subplots(1)

# Make your plot, set your axes labels
ax.plot(sim_1['t'],sim_1['V'],'k')
ax.set_ylabel('V')
ax.set_xlabel('t')

# Turn off tick labels
ax.set_yticklabels([])
ax.set_xticklabels([])

plt.show()

回答by Noel Evans

Without a subplots, you can universally remove the ticks like this:

如果没有subplots,您可以像这样普遍删除刻度线:

plt.xticks([])
plt.yticks([])

回答by MRT

This works great. Just paste this before plt.show():

这很好用。只需粘贴此之前plt.show()

plt.gca().axes.get_yaxis().set_visible(False)

Boom.

繁荣。

回答by Joe Gavin

Not sure this is the best way, but you can certainly replace the tick labels like this:

不确定这是最好的方法,但您当然可以像这样替换刻度标签:

import matplotlib.pyplot as plt
x = range(10)
y = range(10)
plt.plot(x,y)
plt.xticks(x," ")
plt.show()

In Python 3.4 this generates a simple line plot with no tick labels on the x-axis. A simple example is here: http://matplotlib.org/examples/ticks_and_spines/ticklabels_demo_rotation.html

在 Python 3.4 中,这会生成一个简单的线图,在 x 轴上没有刻度标签。一个简单的例子在这里:http: //matplotlib.org/examples/ticks_and_spines/ticklabels_demo_rotation.html

This related question also has some better suggestions: Hiding axis text in matplotlib plots

这个相关问题也有一些更好的建议: Hiding axis text in matplotlib plots

I'm new to python. Your mileage may vary in earlier versions. Maybe others can help?

我是python的新手。您的里程可能会在早期版本中有所不同。也许其他人可以提供帮助?

回答by Ruslan S.

to remove tickmarks entirely use:

要完全删除刻度线,请使用:

ax.set_yticks([])
ax.set_xticks([])

otherwise ax.set_yticklabels([])and ax.set_xticklabels([])will keep tickmarks.

否则ax.set_yticklabels([])ax.set_xticklabels([])将保留刻度线。

回答by Nic Scozzaro

plt.gca().axes.yaxis.set_ticklabels([])

plt.gca().axes.yaxis.set_ticklabels([])

enter image description here

在此处输入图片说明