如何在matplotlib中完全自定义的位置放置次刻度线?

5

如果我想将小刻度线添加到x轴的自定义位置,等同于以下主要刻度命令:plt.xticks([1,2,3])

我尝试过:

import matplotlib.pyplot as plt
x = [1,2,3]
y = [1,4,9]
plt.scatter(x,y)
ax = plt.gca()
ax.xaxis.set_minor_locator([1.1, 1.9, 2.5])

但是这会引发一个错误,因为最后一个命令可能需要一个ticker对象作为参数,而不是一个列表。有没有一种Pythonic的方法来解决这个问题?
1个回答

8

您只需要添加以下一行代码,并将minor标志设置为True

ax.set_xticks([1.1, 1.9, 2.5], minor=True)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接