如果您想理解错误消息,以下信息可能是多余的。请从阅读开始答案 https://stackoverflow.com/a/37968229/6292000
by @用户707650.
使用 MatPlotLib,我想要一个可通用的脚本,可以根据我的数据创建以下内容。
一个窗口包含a子图的排列使得有b每列的子图。我希望能够改变的价值观a and b.
如果我有数据2a子图,我想要 2 个窗口,每个窗口都有前面描述的“a子图按照以下顺序排列b每列的子图”。
我正在绘制的 x 和 y 数据是存储在 np.arrays 中的浮点数,其结构如下:
-
所有绘图的 x 数据始终相同,长度为 5。
'x_vector': [0.000, 0.005, 0.010, 0.020, 0.030, 0.040]
所有图的 y 数据都存储在y_vector其中第一个图的数据存储在索引 0 到 5 处。第二个图的数据存储在索引 6 到 11 处。第三个图的数据为 12-18,第四个图的数据为 19-24,依此类推。
总的来说,对于这个数据集,我有 91 个图(即 y_vector 中存储了 91*6 = 546 个值)。
Attempt:
import matplotlib.pyplot as plt
# Options:
plots_tot = 14 # Total number of plots. In reality there is going to be 7*13 = 91 plots.
location_of_ydata = 6 # The values for the n:th plot can be found in the y_vector at index 'n*6' through 'n*6 + 6'.
plots_window = 7 # Total number of plots per window.
rows = 2 # Number of rows, i.e. number of subplots per column.
# Calculating number of columns:
prim_cols = plots_window / rows
extra_cols = 0
if plots_window % rows > 0:
extra_cols = 1
cols = prim_cols + extra_cols
print 'cols:', cols
print 'rows:', rows
# Plotting:
n=0
x=0
fig, ax = plt.subplots(rows, cols)
while x <= plots_tot:
ax[x].plot(x_vector, y_vector[n:(n+location_of_ydata)], 'ro')
if x % plots_window == plots_window - 1:
plt.show() # New window for every 7 plots.
n = n+location_of_ydata
x = x+1
我收到以下错误:
cols: 4
rows: 2
Traceback (most recent call last):
File "Script.py", line 222, in <module>
ax[x].plot(x_vector, y_vector[n:(n+location_of_ydata)], 'ro')
AttributeError: 'numpy.ndarray' object has no attribute 'plot'