在 matplotlib 图中隐藏轴文本 [英] Hiding axis text in matplotlib plots

查看:38
本文介绍了在 matplotlib 图中隐藏轴文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在任一轴上绘制没有刻度线或数字的图形(我使用传统意义上的轴,而不是 matplotlib 命名法!).我遇到的一个问题是 matplotlib 通过减去值 N 来调整 x(y)ticklabels,然后在轴的末端加上 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()

我想知道的三件事是:

  1. 首先如何关闭此行为(虽然在大多数情况下它很有用,但并不总是如此!)我已经查看了 matplotlib.axis.XAxis 并找不到任何合适的东西

  1. 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))

How can I make N disappear (i.e. 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屋!

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