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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17129947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:33:21  来源:igfitidea点击:

How to set ticks on Fixed Position , matplotlib

pythonmatplotlib

提问by dantebarba

Can anyone help me set the ticks on a fixed position using matplotlib? I've tried using FixedPosition as this tutorial describes:

谁能帮我使用 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

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

But when I try to run, it tells me that set_major_locator method does not exist.

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

A simple example would be very useful.

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

Thanks.

谢谢。

采纳答案by Joe Kington

Just use ax.set_xticks(positions)or ax.set_yticks(positions).

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

For example:

例如:

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

在此处输入图片说明

回答by John Strong

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()