【问题标题】:Modify a specific x-axis tick label in python在python中修改特定的x轴刻度标签
【发布时间】:2016-08-12 13:17:10
【问题描述】:

我是 Python pyplot 的本科生新手。我想做的是针对一个序列绘制一个函数,例如x = [1,2,3,4,5]

pyplot.plot 函数自然给出了一个漂亮的数字。但我想在 x 轴上用字符串“关键点”替换刻度标签“2”,同时使刻度标签,例如“4”和“5”不可见。我怎样才能在pyplot 中实现这一点?

我们将非常感谢您的帮助。

【问题讨论】:

标签: python matplotlib


【解决方案1】:

这就是你的做法:

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,它们将不会显示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    相关资源
    最近更新 更多