Lug*_*cos 2 python matplotlib visual-studio-code
我是一名理学硕士学生,我曾经使用 OriginPro、Excel 和 Matlab 等商业软件包制作图表和绘图。尽管这些软件提供了良好的用户体验,但也存在一些主要缺点,因为它们依赖于特定的操作系统,并且通常非常昂贵。
因此,我开始使用 matplotlib 库和 VS Code 来学习 Python,但是我遇到了一些库函数和语句的问题,这些函数和语句似乎是 matplotlib 和 numPy 的标准,但它不起作用。
例如,我正在为散点图制作一些模板,但我无法控制小刻度,因为它无法识别语句xaxix和yaxix:
代码示例:
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()
Run Code Online (Sandbox Code Playgroud)
并且终端显示此错误:
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'
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
提前致谢!
您写了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))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20543 次 |
| 最近记录: |