Python matplotlib中的子批命令中的索引错误
我不确定这是否是源代码中的一个小错误,但子图不会将两个阵列分散绘制成一块(1018,)的形状。这两个数组都是OLS回归的结果。在使用指定的子图命令之前,我从未遇到过使用变量执行散点图的问题。下面是我的代码:Python matplotlib中的子批命令中的索引错误,python,matplotlib,plot,Python,Matplotlib,Plot,我不确定这是否是源代码中的一个小错误,但子图不会将两个阵列分散绘制成一块(1018,)的形状。这两个数组都是OLS回归的结果。在使用指定的子图命令之前,我从未遇到过使用变量执行散点图的问题。下面是我的代码: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5)) axes[0,0].scatter(Blodgett_wue_obs,Blodgett_wue_results.fittedvalues,color ='blue') ax
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5))
axes[0,0].scatter(Blodgett_wue_obs,Blodgett_wue_results.fittedvalues,color ='blue')
axes[0,0].plot(Blodgett_wue_obs,Blodgett_wue_obs,'r')
File "<ipython-input-311-527571e09d59>", line 1, in <module>
runfile('/Users/JasonDucker/Documents/forJason/Paper_Plots.py', wdir='/Users/JasonDucker/Documents/forJason')
File "/Users/JasonDucker/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Users/JasonDucker/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/Users/JasonDucker/Documents/forJason/Paper_Plots.py", line 362, in <module>
axes[0,0].scatter(Blodgett_wue_obs,Blodgett_wue_results.fittedvalues,color ='blue')
IndexError: too many indices for array
fig,axes=plt.子批次(nrows=1,ncols=3,figsize=(12,5))
轴[0,0]。散布(Blodgett_-wue_obs,Blodgett_-wue_结果。拟合值,color='blue')
轴[0,0]。绘图(Blodgett_wue_obs,Blodgett_wue_obs,'r')
文件“”,第1行,在
运行文件('/Users/JasonDucker/Documents/forJason/Paper_Plots.py',wdir='/Users/JasonDucker/Documents/forJason')
文件“/Users/JasonDucker/anaconda/lib/python2.7/site packages/spyderlib/widgets/externalshell/sitecustomize.py”,第685行,在runfile中
execfile(文件名、命名空间)
文件“/Users/JasonDucker/anaconda/lib/python2.7/site packages/spyderlib/widgets/externalshell/sitecustomize.py”,执行文件第78行
execfile(文件名,*其中)
文件“/Users/JasonDucker/Documents/forJason/Paper_Plots.py”,第362行,在
轴[0,0]。散布(Blodgett_-wue_obs,Blodgett_-wue_结果。拟合值,color='blue')
索引器:数组的索引太多
如果您对这个问题有任何想法,我们将不胜感激 如果指定一行三列,
轴的形状是(3,)
,而不是(1,3)
,因此可以在[0]
,[1]
,[2]
处对图形进行索引
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5))
In [9]: axes.shape
Out[9]: (3,)
获取总是返回二维数组的plt.subplot
的最佳方法是设置squere=False
:
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5), squeeze=False)
In [9]: axes.shape
Out[9]: (1,3)
或者(如果已经将轴
作为一维数组),可以使用:
axes = np.atleast_2d(axes)
比如说,
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5))
In [9]: axes.shape
Out[9]: (3,)
In[10]: axes = np.atleast_2d(axes)
In [11]: axes.shape
Out[11]: (1, 3)
In [12]: axes[0,0]
Out[12]: <matplotlib.axes._subplots.AxesSubplot at 0x1149499d0>
[8]中的fig,axes=plt.子批次(nrows=1,ncols=3,figsize=(12,5))
在[9]中:axes.shape
出[9]:(3,)
[10]中:轴=np.至少\u 2d(轴)
在[11]中:axes.shape
Out[11]:(1,3)
[12]中:轴[0,0]
出[12]:
如果指定一行三列,轴的形状是(3,)
,而不是(1,3)
,因此可以在[0]
,[1]
,[2]
处对图形进行索引
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5))
In [9]: axes.shape
Out[9]: (3,)
获取总是返回二维数组的plt.subplot
的最佳方法是设置squere=False
:
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5), squeeze=False)
In [9]: axes.shape
Out[9]: (1,3)
或者(如果已经将轴
作为一维数组),可以使用:
axes = np.atleast_2d(axes)
比如说,
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5))
In [9]: axes.shape
Out[9]: (3,)
In[10]: axes = np.atleast_2d(axes)
In [11]: axes.shape
Out[11]: (1, 3)
In [12]: axes[0,0]
Out[12]: <matplotlib.axes._subplots.AxesSubplot at 0x1149499d0>
[8]中的fig,axes=plt.子批次(nrows=1,ncols=3,figsize=(12,5))
在[9]中:axes.shape
出[9]:(3,)
[10]中:轴=np.至少\u 2d(轴)
在[11]中:axes.shape
Out[11]:(1,3)
[12]中:轴[0,0]
出[12]:
如果指定一行三列,轴的形状是(3,)
,而不是(1,3)
,因此可以在[0]
,[1]
,[2]
处对图形进行索引
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5))
In [9]: axes.shape
Out[9]: (3,)
获取总是返回二维数组的plt.subplot
的最佳方法是设置squere=False
:
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5), squeeze=False)
In [9]: axes.shape
Out[9]: (1,3)
或者(如果已经将轴
作为一维数组),可以使用:
axes = np.atleast_2d(axes)
比如说,
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5))
In [9]: axes.shape
Out[9]: (3,)
In[10]: axes = np.atleast_2d(axes)
In [11]: axes.shape
Out[11]: (1, 3)
In [12]: axes[0,0]
Out[12]: <matplotlib.axes._subplots.AxesSubplot at 0x1149499d0>
[8]中的fig,axes=plt.子批次(nrows=1,ncols=3,figsize=(12,5))
在[9]中:axes.shape
出[9]:(3,)
[10]中:轴=np.至少\u 2d(轴)
在[11]中:axes.shape
Out[11]:(1,3)
[12]中:轴[0,0]
出[12]:
如果指定一行三列,轴的形状是(3,)
,而不是(1,3)
,因此可以在[0]
,[1]
,[2]
处对图形进行索引
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5))
In [9]: axes.shape
Out[9]: (3,)
获取总是返回二维数组的plt.subplot
的最佳方法是设置squere=False
:
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5), squeeze=False)
In [9]: axes.shape
Out[9]: (1,3)
或者(如果已经将轴
作为一维数组),可以使用:
axes = np.atleast_2d(axes)
比如说,
In [8]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12,5))
In [9]: axes.shape
Out[9]: (3,)
In[10]: axes = np.atleast_2d(axes)
In [11]: axes.shape
Out[11]: (1, 3)
In [12]: axes[0,0]
Out[12]: <matplotlib.axes._subplots.AxesSubplot at 0x1149499d0>
[8]中的fig,axes=plt.子批次(nrows=1,ncols=3,figsize=(12,5))
在[9]中:axes.shape
出[9]:(3,)
[10]中:轴=np.至少\u 2d(轴)
在[11]中:axes.shape
Out[11]:(1,3)
[12]中:轴[0,0]
出[12]:
非常感谢xnx!你的例子做得很好!我没有意识到当你只在轴上输入一行时,物体变成了一维。非常感谢xnx!你的例子做得很好!我没有意识到当你只在轴上输入一行时,物体变成了一维。非常感谢xnx!你的例子做得很好!我没有意识到当你只在轴上输入一行时,物体变成了一维。非常感谢xnx!你的例子做得很好!我没有意识到当你只在轴上输入一行时,物体变成了一维。伟大的未来笔记为我向前迈进。