来自plt.subplots()的轴是"numpy.ndarray".对象并且没有属性"plot". [英] Axes from plt.subplots() is a "numpy.ndarray" object and has no attribute "plot"
问题描述
如果您想了解该错误消息,则下面的信息可能是多余的.请首先阅读答案 由 @ user707650 .
使用MatPlotLib,我想要一个可通用化的脚本,该脚本可从我的数据中创建以下内容.
Using MatPlotLib, I wanted a generalizable script that creates the following from my data.
一个窗口,其中包含 a 子图,因此每列有 b 个子图.我希望能够更改 a 和 b 的值.
A window containing a subplots arranged so that there are b subplots per column. I want to be able to change the values of a and b.
如果我具有 2a 子图的数据,则我需要2个窗口,每个窗口都带有先前描述的" a >按每列 b 子图排列的子图".
If I have data for 2a subplots, I want 2 windows, each with the previously described "a subplots arranged according to b subplots per column".
我正在绘制的x和y数据是存储在np.arrays中的浮点数,其结构如下:
The x and y data I am plotting are floats stored in np.arrays and are structured as follows:
-
所有图的x数据始终相同,且长度为5.
The x data is always the same for all plots and is of length 5.
'x_vector': [0.000, 0.005, 0.010, 0.020, 0.030, 0.040]
所有图的y数据存储在 y_vector 中,其中第一个图的数据存储在索引0到5.第二个图的数据存储在索引6到索引6. 11.第三个情节得到12-18,第四个情节得到19-24,依此类推.
The y data of all plots are stored in y_vector where the data for the first plot is stored at indexes 0 through 5. The data for the second plot is stored at indexes 6 through 11. The third plot gets 12-18, the fourth 19-24, and so on.
对于该数据集,我总共有91个图(即y_vector中存储的91 * 6 = 546个值).
In total, for this dataset, I have 91 plots (i.e. 91*6 = 546 values stored in y_vector).
尝试:
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'
推荐答案
如果仅通过打印ax
来调试程序,就会很快发现ax
是一个二维数组:行,一列.
If you debug your program by simply printing ax
, you'll quickly find out that ax
is a two-dimensional array: one dimension for the rows, one for the columns.
因此,您需要两个索引来索引ax
以检索实际的AxesSubplot
实例,例如:
Thus, you need two indices to index ax
to retrieve the actual AxesSubplot
instance, like:
ax[1,1].plot(...)
如果您想以现在的方式遍历子图,请先展平ax
:
If you want to iterate through the subplots in the way you do it now, by flattening ax
first:
ax = ax.flatten()
现在ax
是一维数组.我不知道是先行还是先行,但是如果错误,请使用转置:
and now ax
is a one dimensional array. I don't know if rows or columns are stepped through first, but if it's the wrong around, use the transpose:
ax = ax.T.flatten()
当然,到目前为止,更简单的方法是即时创建每个子图,因为该子图已经有一个索引,并且其他两个数字是固定的:
Of course, by now it makes more sense to simply create each subplot on the fly, because that already has an index, and the other two numbers are fixed:
for x < plots_tot:
ax = plt.subplot(nrows, ncols, x+1)
注意:您有x <= plots_tot
,但是x
从0开始,您将在当前代码的后面得到一个IndexError
(在展平数组之后). Matplotlib(不幸的是)对子图索引为1.我更喜欢使用索引为0的变量(Python样式),只需为子图索引添加+1
(如上).
Note: you have x <= plots_tot
, but with x
starting at 0, you'll get an IndexError
next with your current code (after flattening your array). Matplotlib is (unfortunately) 1-indexed for subplots. I prefer using a 0-indexed variable (Python style), and just add +1
for the subplot index (like above).
这篇关于来自plt.subplots()的轴是"numpy.ndarray".对象并且没有属性"plot".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!