[英]How to amend matplotlib x-axis ticklabels?
这是一个示例代码:
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()
它创建了这个图:
对于 x 轴,我想将 x 轴刻度标签修改为仅显示 1、2、3、4、5,而我想清除 0 和 6。我尝试ax.set_xticklabels([" ",1,2,3,4,5," "])
但这不起作用。 我得到了这个结果:
这个警告信息:
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
如何修改 x 轴刻度标签以仅显示 1、2、3、4、5?
您可以在调用ax.set_xticklabels
ax.set_xticks
之前使用 ax.set_xticks 设置 xticks。 请参见下面的代码:
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()
output 给出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.