gpt4 book ai didi

python - 如何解决“"' PathCollection'对象没有属性 'yaxis'”错误?

转载 作者:行者123 更新时间:2023-12-03 08:50:12 25 4
gpt4 key购买 nike

我是一名理学硕士学生,我曾经使用 OriginPro、Excel 和 Matlab 等商业软件包制作图表和绘图。尽管这些软件提供了出色的用户体验,但也存在一些主要缺点,因为它们依赖于特定的操作系统,而且通常非常昂贵。

因此,我开始使用 matplotlib 库和 VS Code 来学习 Python,但是我在某些库函数和语句方面遇到了一些问题,这些函数和语句似乎是 matplotlib 和 numPy 的标准,但它不起作用。

例如,我正在为散点图制作一些模板,但我无法控制小刻度,因为它无法识别语句 xaxixyaxix:

代码示例:

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

.
.
.

fig = plt.figure(figsize=(x_pixels/my_dpi, y_pixels/my_dpi), dpi=my_dpi)
ax = plt.scatter(x*format_x, y*format_y, s = size, alpha = transparency, color = color, label = legend_text)

.
.
.

# Major Ticks
plt.tick_params(axis = 'both', which = 'major', length = majorT_length, direction = majorT_direction, color = majorT_color, labelsize = label_size, top = 'on', right = 'on')

# Minor Ticks
plt.minorticks_on()
plt.tick_params(axis='both', which='minor', length = minorT_length, direction = minorT_direction, color = minorT_color, top = 'on', right = 'on')
ax.yaxis.set_minor_locator(AutoMinorLocator(2))
ax.xaxis.set_minor_locator(AutoMinorLocator(2))

# Figure Layout
plt.tight_layout()
plt.savefig(output_file, dpi=my_dpi, bbox_inches=borders)

plt.show()

并且终端显示此错误:

enter image description here

  File "c:/Users/luagu/Desktop/Python Matplotlib Training/Scatter_Template.py", line 128, in <module>
ax.yaxis.set_minor_locator(AutoMinorLocator(2))
AttributeError: 'PathCollection' object has no attribute 'yaxis'

我做错了什么?

提前致谢!

最佳答案

您写了ax = plt.scatter但是你的ax这是 scatter 返回的艺术家方法,而不是Axes目的。你想做的是:

plt.scatter(...)
...
ax = plt.gca()
ax.yaxis.set_minor_locator(AutoMinorLocator(2))
ax.xaxis.set_minor_locator(AutoMinorLocator(2))

关于python - 如何解决“"' PathCollection'对象没有属性 'yaxis'”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59440583/

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