如何在固定位置设置刻度,matplotlib
- 2025-04-10 09:46:00
- admin 原创
- 18
问题描述:
有人能帮我用 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 方法不存在。
一个简单的例子就会非常有用。
谢谢。
解决方案 1:
只需使用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()
解决方案 2:
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()
相关推荐
热门文章
项目管理软件有哪些?
热门标签
曾咪二维码
扫码咨询,免费领取项目管理大礼包!
云禅道AD