Dia*_*rox 6 widget matplotlib python-3.x anaconda jupyter-notebook
我在一个函数中使用子图,该函数使用滑块小部件输入来计算一些东西并绘制结果。我想打开 ax1 的所有子图的网格。但不知何故 jupternotebooks 只在最后一个情节上打开它......
import numpy as np
from matplotlib import pyplot as plt
import ipywidgets as widgets
from IPython.html.widgets import interact
%matplotlib inline
## Plot
fig, ax1 = plt.subplots(6,2)
plt.subplots_adjust(right = 2, top = 8 )
# Show the major grid lines with dark grey lines
plt.grid(b=True, which='major', color='#666666', linestyle='-')
# Show the minor grid lines with very faint and almost transparent grey lines
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
## Giergeschwindigkeit über v und ay
ax1[0,0].plot(v_ms, omega)
ax1[0,0].set_ylabel('Giergeschwindigkeit [rad/s]')
ax1[0,0].set_xlabel('Geschwindigkeit [m/s]')
ax1[0,0].set_title('Giergeschwindigkeit über Geschwindigkeit')
# ... more subplots
plt.show()
Run Code Online (Sandbox Code Playgroud)
你能向我解释为什么在我的情况下
ax1.grid()
Run Code Online (Sandbox Code Playgroud)
抛出错误?
AttributeError: 'numpy.ndarray' object has no attribute 'grid'
Run Code Online (Sandbox Code Playgroud)
这是因为plt只会对最后创建的轴对象进行操作。
您收到该错误的原因是这ax1是一个 numpy n 维数组,而不是轴对象。
您可以这样做来迭代 numpy n 维数组以创建网格:
for row in axes:
for ax in row:
ax.grid(b=True, which='major', color='#666666', linestyle='-')
ax.minorticks_on()
ax.grid(b=True, which='minor', color='#999999', linestyle='-',alpha=0.2)
Run Code Online (Sandbox Code Playgroud)