将Python设置为自定义设置的y轴次刻度标记频率

时间:2015-05-26 22:02:53

标签: python-2.7 matplotlib axis-labels

这个问题与在Python图中用y轴绘制小蜱火星有关 matplotlib。

以下是我的代码:

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

fig, ax = plt.subplots()
fig.set_facecolor('white')

x = [1,2,3]
plt.subplot(211)
plt.plot([1,2,3], label="test1")
plt.plot([3,2,1], label="test2")
plt.xticks()
plt.yticks()
ax.yaxis.set_minor_locator(MultipleLocator(5))
plt.show()

当我生成这个情节时,我没有得到任何小的刻度。

我附上了这段代码给我的情节。 enter image description here

我可以在这里显示y轴的次要刻度吗?

1 个答案:

答案 0 :(得分:1)

您可以在plt.yticks()中设置所需的刻度,输入可以是您事先生成的numpy数组

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np

fig, ax = plt.subplots()
fig.set_facecolor('white')

yticks = np.arange(1,3,0.2)

x = [1,2,3]
plt.subplot(211)
plt.plot([1,2,3], label="test1")
plt.plot([3,2,1], label="test2")
plt.xticks()
plt.yticks(yticks)
ax.yaxis.set_minor_locator(MultipleLocator(5))
plt.show()

给你:

enter image description here