[英]How to amend matplotlib x-axis ticklabels?
Here is a sample code:这是一个示例代码:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
y = np.sin(x)
ax.plot(x, y, marker='s')
ax.set_xlabel('angle')
ax.set_title('sine')
#ax.set_xticklabels([" ",1,2,3,4,5," "]) # don't work
ax.set_yticks([-1,0,1])
plt.show()
It creates this Figure:它创建了这个图:
For the x-axis, I would like to amend the x-axis ticklabels to only show 1,2,3,4,5 while I would like to blank-out 0 and 6. I tried ax.set_xticklabels([" ",1,2,3,4,5," "])
but this did not work.对于 x 轴,我想将 x 轴刻度标签修改为仅显示 1、2、3、4、5,而我想清除 0 和 6。我尝试
ax.set_xticklabels([" ",1,2,3,4,5," "])
但这不起作用。 I get this outcome instead:我得到了这个结果:
and this warning msg:这个警告信息:
Warning (from warnings module):
File "~/test.py", line 12
ax.set_xticklabels(["",1,2,3,4,5,""]) # don't work
UserWarning: FixedFormatter should only be used together with FixedLocator
How do I amend the x-axis ticklabels to only show 1,2,3,4,5?如何修改 x 轴刻度标签以仅显示 1、2、3、4、5?
You could just set the xticks with ax.set_xticks
before calling your ax.set_xticklabels
function.您可以在调用
ax.set_xticklabels
ax.set_xticks
之前使用 ax.set_xticks 设置 xticks。 See code below:请参见下面的代码:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
y = np.sin(x)
ax.plot(x, y, marker='s')
ax.set_xlabel('angle')
ax.set_title('sine')
ax.set_xticks(np.arange(7))
ax.set_xticklabels([" ",1,2,3,4,5," "])
ax.set_yticks([-1,0,1])
plt.show()
And the output gives: output 给出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.