在 matplotlib 图中隐藏轴文本
我试图在任一轴上绘制一个没有刻度线或数字的图形(我使用传统意义上的轴,而不是 matplotlib 命名法!)。我遇到的一个问题是 matplotlib 通过减去值 N 来调整 x(y)ticklabels,然后在轴的末尾添加 N。
这可能很模糊,但下面的简化示例突出了这个问题,其中“6.18”是 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
并且找不到任何合适的内容如何使 N 消失(即
X. set_visible(False)
)有没有更好的方法来执行上述操作?如果相关的话,我的最终绘图将是图中的 4x4 子图。
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.
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()
The three things I would like to know are:
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 appropriateHow can I make N disappear (i.e.
X.set_visible(False)
)Is there a better way to do the above anyway? My final plot would be 4x4 subplots in a figure, if that is relevant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果您只想隐藏轴文本并保留网格线:
执行
set_visible(False)
或set_ticks([])
也会隐藏网格线。If you want to hide just the axis text keeping the grid lines:
Doing
set_visible(False)
orset_ticks([])
will also hide the grid lines.如果您像我一样,在绘制图形时并不总是检索轴,
ax
,那么一个简单的解决方案是If you are like me and don't always retrieve the axes,
ax
, when plotting the figure, then a simple solution would be to do我已经对这个图进行了颜色编码以简化该过程。
您可以使用这些命令完全控制图形,为了完成答案,我还添加了对脊柱的控制:
I've colour coded this figure to ease the process.
You can have full control over the figure using these commands, to complete the answer I've add also the control over the spines:
我实际上无法根据此处的任何代码片段(甚至是答案中接受的代码片段)渲染没有边框或轴数据的图像。在深入研究了一些 API 文档后,我找到了这段代码来渲染我的图像,
我使用
tick_params
调用基本上关闭了可能渲染的任何额外信息,并且我的输出文件中有一个完美的图形。I was not actually able to render an image without borders or axis data based on any of the code snippets here (even the one accepted at the answer). After digging through some API documentation, I landed on this code to render my image
I used the
tick_params
call to basically shut down any extra information that might be rendered and I have a perfect graph in my output file.有点旧线程,但是,这似乎是使用最新版本的 matplotlib 的更快方法:
设置 x 轴的主要格式化程序
Somewhat of an old thread but, this seems to be a faster method using the latest version of matplotlib:
set the major formatter for the x-axis
一种技巧可能是将刻度标签的颜色设置为白色以隐藏它!
或者更一般化(@Armin Okić),您可以将其设置为
“None”
。One trick could be setting the color of tick labels as white to hide it!
or to be more generalized (@Armin Okić), you can set it as
"None"
.使用面向对象的 API 时,
Axes
对象有两个有用的方法用于删除轴文本:set_xticklabels()
和set_xticks()
。假设您使用以下方法创建绘图。
如果您只是想删除刻度标签,则可以使用
或完全删除刻度,您可以使用
这些方法对于准确指定您想要刻度的位置以及如何标记它们非常有用。传递空列表将分别导致没有刻度或没有标签。
When using the object oriented API, the
Axes
object has two useful methods for removing the axis text,set_xticklabels()
andset_xticks()
.Say you create a plot using
If you simply want to remove the tick labels, you could use
or to remove the ticks completely, you could use
These methods are useful for specifying exactly where you want the ticks and how you want them labeled. Passing an empty list results in no ticks, or no labels, respectively.
您可以简单地将
xlabel
设置为None
,直接在您的轴上。下面是使用seaborn的工作示例You could simply set
xlabel
toNone
, straight in your axis. Below an working example using seaborn如果你有子图,就这样做
Just do this in case you have subplots
如何让刻度标签保留但轴标签消失:
axs.xaxis.label.set_visible(False)
How to let the tick labels stay but the axis label go away:
axs.xaxis.label.set_visible(False)
尝试使用以下代码行:
应该可以为您解决问题。
Try with the following line of code:
Should solve the issue for you.
要保持刻度线和网格线完整但隐藏刻度标签,请使用:
这还有一个优点,即 Jupyter Notebook 交互式后端的鼠标悬停实用程序不受影响;大多数其他解决方案往往会导致鼠标悬停实用程序失效,即不显示绘图坐标。
To keep the ticks and grid lines intact but hide the tick labels, use:
This also has the advantage that the mouse-over utility of the interactive backend of Jupyter Notebook is not affected; most of the other solutions tend to cause the mouse-over utility to defunct, i.e. not showing the plot coordinates.
您可以隐藏整个轴,而不是隐藏每个元素:
或者,您可以将刻度设置为空列表:
在第二个选项中,您仍然可以使用
plt.xlabel()
和plt.ylabel()
向轴添加标签。Instead of hiding each element, you can hide the whole axis:
Or, you can set the ticks to an empty list:
In this second option, you can still use
plt.xlabel()
andplt.ylabel()
to add labels to the axes.