gpt4 book ai didi

python - 在 python 中使用 matplotlib 设置刻度位置时出错

转载 作者:行者123 更新时间:2023-11-28 20:27:29 24 4
gpt4 key购买 nike

我正在尝试在 matplotlib 中设置刻度线位置。运行以下最小示例时出现错误:

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

x = 10.*np.random.randn(1000)
y = 10.*np.random.randn(1000)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x, y)
ax.xaxis.set_major_formatter(MultipleLocator(1.))
ax.yaxis.set_major_formatter(MultipleLocator(1.))
plt.show()

错误在于设置 x 轴和 y 轴刻度线的两条线。如果我改用 NullFormatter() 或完全省略这些行,则代码运行良好并生成预期的图。但是,上面的代码返回以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
return self.func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 245, in resize
self.show()
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 248, in draw
FigureCanvasAgg.draw(self)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 394, in draw
self.figure.draw(self.renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 798, in draw
func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 1946, in draw
a.draw(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 971, in draw
tick_tups = [ t for t in self.iter_ticks()]
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 906, in iter_ticks
self.major.formatter.set_locs(majorLocs)
AttributeError: MultipleLocator instance has no attribute 'set_locs'

我试过用谷歌搜索这个错误,但我找不到其他有类似问题的人。关于为什么使用定位器会产生错误的任何想法?

最佳答案

MultipleLocator 是定位器,而不是格式化程序。你想用

ax.xaxis.set_major_locator(MultipleLocator(1.))
ax.yaxis.set_major_locator(MultipleLocator(1.))

这对我有用(使用 1 看起来不太漂亮,但它有效)。

关于python - 在 python 中使用 matplotlib 设置刻度位置时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8548199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com