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

dan*_*rba 27 python matplotlib

任何人都可以帮我用matplotlib设置固定位置的刻度线吗?我尝试过使用FixedPosition,因为本教程描述了:

ax = pl.gca()
ax.xaxis.set_major_locator(eval(locator))
Run Code Online (Sandbox Code Playgroud)

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

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

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

谢谢.

Joe*_*ton 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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 这个方法将删除我在图上的现有刻度.如果我想添加这些额外的刻度而不影响现有的刻度怎么办? (3认同)

Joh*_*ong 5

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()
Run Code Online (Sandbox Code Playgroud)