在Matplotlib图中隐藏轴文本 [英] Hiding axis text in matplotlib plots
问题描述
我试图在两个轴上绘制没有刻度线或数字的图形(我使用传统意义上的轴,而不是matplotlib命名法!).我遇到的一个问题是,matplotlib通过减去值N来调整x(y)ticklabel,然后在轴的末端添加N.
I'm trying to plot a figure without tickmarks or numbers on either of the axes (I use axes in the traditional sense, not the matplotlib nomenclature!). An issue I have come across is where matplotlib adjusts the x(y)ticklabels by subtracting a value N, then adds N at the end of the axis.
这可能含糊不清,但是下面的简化示例突出了这个问题,其中"6.18"是N的有问题的值:
This may be vague, but the following simplified example highlights the issue, with '6.18' being the offending value of N:
import matplotlib.pyplot as plt
import random
prefix = 6.18
rx = [prefix+(0.001*random.random()) for i in arange(100)]
ry = [prefix+(0.001*random.random()) for i in arange(100)]
plt.plot(rx,ry,'ko')
frame1 = plt.gca()
for xlabel_i in frame1.axes.get_xticklabels():
xlabel_i.set_visible(False)
xlabel_i.set_fontsize(0.0)
for xlabel_i in frame1.axes.get_yticklabels():
xlabel_i.set_fontsize(0.0)
xlabel_i.set_visible(False)
for tick in frame1.axes.get_xticklines():
tick.set_visible(False)
for tick in frame1.axes.get_yticklines():
tick.set_visible(False)
plt.show()
我想知道的三件事是:
-
如何首先关闭此行为(尽管在大多数情况下它很有用,但并非总是如此!)我已经浏览了
matplotlib.axis.XAxis
,却找不到合适的东西
How to turn off this behaviour in the first place (although in most cases it is useful, it is not always!) I have looked through
matplotlib.axis.XAxis
and cannot find anything appropriate
如何使N消失(即X.set_visible(False)
)
是否有更好的方法来执行上述操作?如果可以的话,我的最终绘图将是图形中的4x4子图.
Is there a better way to do the above anyway? My final plot would be 4x4 subplots in a figure, if that is relevant.
推荐答案
除了隐藏每个元素,您还可以隐藏整个轴:
Instead of hiding each element, you can hide the whole axis:
frame1.axes.get_xaxis().set_visible(False)
frame1.axes.get_yaxis().set_visible(False)
或者,您可以将刻度线设置为空列表:
Or, you can set the ticks to an empty list:
frame1.axes.get_xaxis().set_ticks([])
frame1.axes.get_yaxis().set_ticks([])
在第二个选项中,您仍然可以使用plt.xlabel()
和plt.ylabel()
在轴上添加标签.
In this second option, you can still use plt.xlabel()
and plt.ylabel()
to add labels to the axes.
这篇关于在Matplotlib图中隐藏轴文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!