Python 在 matplotlib 中设置刻度标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40918922/
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
Set tick labels in matplotlib
提问by godaygo
I'm trying to make some manipulations with tick labels in matplotlib. But it seems that code does not do what I want, and I have no idea why? There is no labels.
我正在尝试使用 matplotlib 中的刻度标签进行一些操作。但似乎代码没有做我想要的,我不知道为什么?没有标签。
In[1]: import matplotlib.pyplot as plt
import numpy as np
In[2]: x = np.arange(0,10)
In[3]: plt.plot(x,x)
locs, labs = plt.xticks()
plt.xticks(locs[1:], labs[1:])
plt.show()
Any help please! What I want is to delete first label on x axis:
I'm using:
python 3.5.2
matplotlib 1.5.3
win 10
请任何帮助!我想要的是删除 x 轴上的第一个标签:
我正在使用:
python 3.5.2
matplotlib 1.5.3
win 10
回答by Julien Spronck
fig, ax = plt.subplots()
ax.plot(x, x)
ticks = ax.get_xticks()
ax.set_xticks(ticks[1:])
plt.show()
回答by Bill Bell
This is a direct answer to your question, rather than a remedy to your predicament.
这是对您问题的直接回答,而不是对您的困境的补救措施。
>>> locs, labs = plt.xticks()
>>> locs
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
>>> labs
<a list of 10 Text xticklabel objects>
Notice that when you make a getcall to xticksit yields a one-dimensional array of locations for tick marks and a list of objects. However, when you make a putcall to xticksit expects an array-like collection of locations and an array-like collection of strings. For some reason, xticksseems to have failed to croak upon being given that list of objects.
请注意,当您对xticks进行get调用时,它会生成一个一维的刻度线位置数组和一个对象列表。但是,当您对xticks进行put调用时,它需要一个类似数组的 location 集合和一个类似数组的strings集合。出于某种原因,xticks在获得该对象列表时似乎没有发出声音。