plt.subplots() 中的轴是“numpy.ndarray";对象并且没有属性“情节" [英] Axes from plt.subplots() is a "numpy.ndarray" object and has no attribute "plot"
问题描述
如果您想了解错误消息,以下信息可能是多余的.请先阅读答案来自 @user707650.
The information below may be superfluous if you are trying to understand the error message. Please start off by reading the answer by @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 到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 个图(即 91*6 = 546 个存储在 y_vector 中的值).
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";对象并且没有属性“情节"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!