Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我总是得到这个错误:`ValueError:数组的真值。。。使用a.any()或a.all()`以及如何修复它?_Python_Python 3.x_Matplotlib_Seaborn_Subplot - Fatal编程技术网

Python 为什么我总是得到这个错误:`ValueError:数组的真值。。。使用a.any()或a.all()`以及如何修复它?

Python 为什么我总是得到这个错误:`ValueError:数组的真值。。。使用a.any()或a.all()`以及如何修复它?,python,python-3.x,matplotlib,seaborn,subplot,Python,Python 3.x,Matplotlib,Seaborn,Subplot,我一直遇到这个ValueError:ValueError:包含多个元素的数组的真值不明确。使用软件包proplot创建绘图时,使用a.any()或a.all(),同样,尝试使用matplotlib复制绘图时,也应该使用matplotlib 例如,当我试图在中复制图形时,我遇到了错误。但是问题已经解决了,所以我觉得这些情节应该有效 将熊猫作为pd导入 将xarray作为xr导入 将numpy作为np导入 导入seaborn作为sns 将proplot导入为绘图 导入日历 def drop\u na

我一直遇到这个ValueError:
ValueError:包含多个元素的数组的真值不明确。使用软件包
proplot
创建绘图时,使用a.any()或a.all()
,同样,尝试使用matplotlib复制绘图时,也应该使用matplotlib

例如,当我试图在中复制图形时,我遇到了错误。但是问题已经解决了,所以我觉得这些情节应该有效

将熊猫作为pd导入
将xarray作为xr导入
将numpy作为np导入
导入seaborn作为sns
将proplot导入为绘图
导入日历
def drop\u nans\u和\u flant(dataArray:xr.dataArray)->np.ndarray:
“”“展平数组并从该数组中删除NaN。用于绘制直方图。
论据:
---------
:dataArray(xr.dataArray)
要展平的值的DataArray
"""
#放下南斯,把它压平
返回dataArray.values[~np.isnan(dataArray.values)]
#创建xarray对象的尺寸
时间=pd.日期\范围(开始时间='1981-01-31',结束时间='2019-04-30',频率='M')
lat=np.linspace(0,1224)
lon=np.linspace(0,1176)
rand_arr=np.random.randn(len(次),len(lat),len(lon))
#创建xr.Dataset
coords={'time':times'lat':lat'lon':lon}
dims=[“时间”、“纬度”、“经度”]
ds=xr.Dataset({'precip':(dims,rand_arr)},coords=coords)
ds['month'],ds['year']=ds['time.month'],ds['time.year']
f、 axs=绘图。子绘图(nrows=4,ncols=3,axwidth=1.5,figsize=(8,12),share=2)#share=3,span=1,
axs.format(
xlabel='Precip',ylabel='Density',suptitle='Distribution',
)
月份缩写=列表(日历.月份缩写)
平均值\u ds=ds.groupby('time.month')。平均值(dim='time')
展平=[]
对于np.arange(1,13)中的mth:
ax=axs[mth-1]
ax.set_title(月缩写[mth])
打印(f“绘图{month_abbrs[mth]}”)
平坦=下降和平坦(平均月数=月数).precip)
展平。追加(展平)
sns.distplot(平面,ax=ax,**{'kde':False})
这是错误和输出:

Plotting Jan
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-877b47c30863> in <module>
     45     flat = drop_nans_and_flatten(mean_ds.sel(month=mth).precip)
     46     flattened.append(flat)
---> 47     sns.distplot(flat, ax=ax, **{'kde': False})

/opt/anaconda3/envs/maize-Toff/lib/python3.8/site-packages/seaborn/distributions.py in distplot(a, bins, hist, kde, rug, fit, hist_kws, kde_kws, rug_kws, fit_kws, color, vertical, norm_hist, axlabel, label, ax)
    226         ax.hist(a, bins, orientation=orientation,
    227                 color=hist_color, **hist_kws)
--> 228         if hist_color != color:
    229             hist_kws["color"] = hist_color
    230 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
45平坦=下降和平坦(平均月数=月数).precip)
46.平展追加(平展)
--->47 sns.distplot(平面,ax=ax,***{'kde':False})
/距离图中的opt/anaconda3/envs/玉蜀黍Toff/lib/python3.8/site-packages/seaborn/distributions.py(a、箱子、历史、kde、地毯、适合、地毯、适合、颜色、垂直、标准历史、axlabel、标签、ax)
226 ax.历史(a,箱子,方向=方向,
227颜色=历史颜色,**历史颜色(kws)
-->228如果历史颜色!=颜色:
229历史颜色kws[“颜色”]=历史颜色
230
ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()
我使用的proplot版本是:0.5.0

我所尝试的。读取值错误输出看起来所绘制内容的数据格式是错误的,但当我尝试将.any()或.all()添加到变量precip或flat时,仍然无法工作


我不确定它是否是固定的,但您可以提到绘图的特定颜色,如下所示

sns.distplot(flat, ax=ax, color = 'r', kde = False)

以消除错误。”color=None(默认参数)似乎给出了错误。

Wow,就是这样。真不敢相信这是一个如此直截了当的解决办法。非常感谢。很高兴能帮上忙。你现在可以专注于你的实际研究了:)-来自前研究生。