Python 隐藏刻度但显示刻度标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29988241/
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
Python hide ticks but show tick labels
提问by user308827
I can remove the ticks with
我可以用
ax.set_xticks([])
ax.set_yticks([])
but this removes the labels as well. Any way I can plot the tick labels but not the ticks and the spine
但这也会删除标签。任何方式我都可以绘制刻度标签但不能绘制刻度和脊椎
采纳答案by Julien Spronck
You can set the tick length to 0 using tick_params
(http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.tick_params):
您可以使用tick_params
(http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.tick_params)将刻度长度设置为 0 :
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1],[1])
ax.tick_params(axis=u'both', which=u'both',length=0)
plt.show()
回答by cmidi
matplotlib.pyplot.setp(*args, **kwargs)
is used to set properties of an artist object. You can use this in addition to get_xticklabes()
to make it invisible.
matplotlib.pyplot.setp(*args, **kwargs)
用于设置艺术家对象的属性。除了get_xticklabes()
使其不可见之外,您还可以使用它。
something on the lines of the following
以下内容
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(2,1,1)
ax.set_xlabel("X-Label",fontsize=10,color='red')
plt.setp(ax.get_xticklabels(),visible=False)
Below is the reference page http://matplotlib.org/api/pyplot_api.html
回答by mab
Thanks for your answers @julien-spronck and @cmidi.
As a note, I had to use both methods to make it work:
感谢您的回答@julien-spronck 和@cmidi。
请注意,我必须同时使用这两种方法才能使其工作:
import numpy as np
import matplotlib.pyplot as plt
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(11, 3))
data = np.random.random((4, 4))
ax1.imshow(data)
ax1.set(title='Bad', ylabel='$A_y$')
# plt.setp(ax1.get_xticklabels(), visible=False)
# plt.setp(ax1.get_yticklabels(), visible=False)
ax1.tick_params(axis='both', which='both', length=0)
ax2.imshow(data)
ax2.set(title='Somewhat OK', ylabel='$B_y$')
plt.setp(ax2.get_xticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
# ax2.tick_params(axis='both', which='both', length=0)
ax3.imshow(data)
ax3.set(title='Nice', ylabel='$C_y$')
plt.setp(ax3.get_xticklabels(), visible=False)
plt.setp(ax3.get_yticklabels(), visible=False)
ax3.tick_params(axis='both', which='both', length=0)
plt.show()
回答by Marcelo Villa-Pi?eros
You can set the yaxis
and xaxis
set_ticks_position
properties so they just show on the left and bottom sides, respectively.
您可以设置yaxis
和xaxis
set_ticks_position
属性,使它们分别显示在左侧和底部。
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
Furthermore, you can hide the spines as well by setting the set_visible
property of the specific spine to False
.
此外,您还可以通过将set_visible
特定脊椎的属性设置为 来隐藏脊椎False
。
axes[i].spines['right'].set_visible(False)
axes[i].spines['top'].set_visible(False)
回答by Marcus
While attending a coursera course on Python, this was a question.
在参加有关 Python 的课程时,这是一个问题。
Below is the given solution, which I think is more readable and intuitive.
下面是给定的解决方案,我认为它更具可读性和直观性。
ax.tick_params(top='off', bottom='off', left='off', right='off', labelleft='on', labelbottom='on')
回答by Amir
Assuming that you want to remove some ticks on the Y
axes and only show the yticks
that correspond to the ticks that have values higher than 0 you can do the following:
假设您想删除Y
轴上的一些刻度并仅显示yticks
对应于值高于 0 的刻度的 ,您可以执行以下操作:
from import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# yticks and yticks labels
yTicks = list(range(26))
yTicks = [yTick if yTick % 5 == 0 else 0 for yTick in yTicks]
yTickLabels = [str(yTick) if yTick % 5 == 0 else '' for yTick in yTicks]
Then you set up your axis object's Y
axes as follow:
然后Y
按如下方式设置轴对象的轴:
ax.yaxis.grid(True)
ax.set_yticks(yTicks)
ax.set_yticklabels(yTickLabels, fontsize=6)
fig.savefig('temp.png')
plt.close()
And you'll get a plot like this:
你会得到一个这样的情节:
回答by Valerie
This worked for me:
这对我有用:
plt.tick_params(axis='both', labelsize=0, length = 0)