如何在固定位置,matplotlib上设置刻度

时间:2013-06-16 03:33:21

标签: python matplotlib

有人可以帮我用matplotlib在固定位置上设置滴答声吗?我尝试过使用FixedPosition,因为本教程描述了:

ax = pl.gca()
ax.xaxis.set_major_locator(eval(locator))

http://scipy-lectures.github.io/intro/matplotlib/matplotlib.html#figures-subplots-axes-and-ticks

但是当我尝试运行时,它告诉我set_major_locator方法不存在。

一个简单的例子非常有用。

感谢。

2 个答案:

答案 0 :(得分:52)

只需使用ax.set_xticks(positions)ax.set_yticks(positions)

例如:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.set_xticks([0.15, 0.68, 0.97])
ax.set_yticks([0.2, 0.55, 0.76])
plt.show()

enter image description here

答案 1 :(得分:3)

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

name_list = ('Omar', 'Serguey', 'Max', 'Zhou', 'Abidin')
value_list = np.random.randint(0, 99, size = len(name_list))
pos_list = np.arange(len(name_list))

ax = plt.axes()
ax.xaxis.set_major_locator(ticker.FixedLocator((pos_list)))
ax.xaxis.set_major_formatter(ticker.FixedFormatter((name_list)))
plt.bar(pos_list, value_list, color = '.75', align = 'center')

plt.show()