如何仅标记某些 x-ticks matplotlib?

How to label only certain x-ticks matplotlib?

在此图中,我分配了刻度并应用了刻度:

ax.set_xticks(np.arange(0, 800, 1))
ax.grid()

如何在 x 轴上仅标记图例中的数字?

此致,

卡尔

试试这个保留所有网格点,但只标记兴趣点。

original_labels = [str(label) for label in ax.get_xticks()]
labels_of_interest = [str(i) for i in np.arange(235,295,5)]
new_labels = [label if label in labels_of_interest else "" for label in original_labels]
ax.set_xticklabels(new_labels)

编辑:

ax.set_xticklabels 的可选参数是 rotation。如果您发现标签仍然重叠,请尝试旋转它们:

ax.set_xticklabels(new_labels, rotation=45) #rotate labels 45 degrees