在python中修改特定的x轴刻度标签
我是Python pyplot的本科生新手。我想做的是根据一个序列绘制一个函数,例如,在python中修改特定的x轴刻度标签,python,matplotlib,Python,Matplotlib,我是Python pyplot的本科生新手。我想做的是根据一个序列绘制一个函数,例如,x=[1,2,3,4,5] pyplot.plot函数自然会给出一个漂亮的图形。但是我想用字符串替换x轴上的记号标签“2”,比如说“关键点”,同时使记号标签不可见,例如,“4”和“5”。如何在pyplot中实现这一点 我们将非常感谢你的帮助 这就是你的做法: from matplotlib import pyplot as plt x = [1,2,3,4,5] y = [1,2,0,2,1] plt.cl
x=[1,2,3,4,5]
pyplot.plot
函数自然会给出一个漂亮的图形。但是我想用字符串替换x轴上的记号标签“2”,比如说“关键点”,同时使记号标签不可见,例如,“4”和“5”。如何在pyplot
中实现这一点
我们将非常感谢你的帮助 这就是你的做法:
from matplotlib import pyplot as plt
x = [1,2,3,4,5]
y = [1,2,0,2,1]
plt.clf()
plt.plot(x,y,'o-')
ax = plt.gca() # grab the current axis
ax.set_xticks([1,2,3]) # choose which x locations to have ticks
ax.set_xticklabels([1,"key point",2]) # set the labels to display at those ticks
通过从xtick列表中省略4和5,它们将不会显示。这是您的操作方式:
from matplotlib import pyplot as plt
x = [1,2,3,4,5]
y = [1,2,0,2,1]
plt.clf()
plt.plot(x,y,'o-')
ax = plt.gca() # grab the current axis
ax.set_xticks([1,2,3]) # choose which x locations to have ticks
ax.set_xticklabels([1,"key point",2]) # set the labels to display at those ticks
通过从xtick列表中省略4和5,它们将不会显示。欢迎使用堆栈溢出:-)请查看以及如何创建堆栈溢出。这将有助于获得有用的答案。欢迎使用堆栈溢出:-)请查看以及如何创建堆栈溢出。这将有助于得到有用的答案。