【问题标题】:plt.fill_between not working in pythonplt.fill_between 在 python 中不起作用
【发布时间】:2017-09-28 10:43:36
【问题描述】:

我在 python 中使用以下代码(iris、numpy 和 matplotlib)(其中 CCCma、CRU 和 UDel 都是预定义的多维数据集):

#We want to plot the mean for the whole region so we need a mean of all the lats and lons
CCCma_mean = CCCma.collapsed(['latitude', 'longitude'], iris.analysis.MEAN, weights=CCCma_grid_areas)
CRU_mean = CRU.collapsed(['latitude', 'longitude'], iris.analysis.MEAN, weights=CRU_grid_areas)                                              
UDel_mean = UDel.collapsed(['latitude', 'longitude'], iris.analysis.MEAN, weights=UDel_grid_areas)                                                 


#PART 4: PLOT LINE GRAPH  
#limit x axis    
plt.xlim((1990,2008))    

#assign the line colours
qplt.plot(CCCma_mean.coord('year'), CCCma_mean, label='CanRCM4_ERAINT', lw=1.5, color='blue')
qplt.plot(CRU_mean.coord('year'), CRU_mean, label='Observed_CRU', lw=2, color='black')
qplt.plot(UDel_mean.coord('year'), UDel_mean, label='Observed_UDel', lw=2, color='black', linestyle='--')

#create a legend and set its location to under the graph
plt.legend(loc="upper center", bbox_to_anchor=(0.5,-0.05), fancybox=True, shadow=True, ncol=2)

#create a title
plt.title('Mean Near Surface Temperature for Malawi 1989-2008', fontsize=11)   

#add grid lines
plt.grid()

#save the image of the graph and include full legend
#plt.savefig('ERAINT_Temperature_LineGraph_Annual', bbox_inches='tight')

#show the graph in the console
iplt.show()

这创建了这个漂亮的图表

我想做的是填充两个观察到的数据集之间的区域(阴影)。我已尝试添加此代码:

#fill error area for observed data
x = np.arange(1990,2008,1)    
plt.fill_between(x, CRU_mean, UDel_mean, color='grey', alpha='0.5')

但是当我这样做时,没有添加填充,图例消失并且出现此错误:

  File "/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python_Code_and_Output_Images/Templates/Line_Graph_Tas_Template_Annual_Only.py", line 116, in main
    plt.fill_between(x, CRU_mean, UDel_mean, color='grey', alpha='0.5')

  File "/scratch/s0XXXX/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2896, in fill_between
    **kwargs)

  File "/scratch/s0XXXX/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py", line 1819, in inner
    return func(ax, *args, **kwargs)

  File "/scratch/s0XXXX/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 4582, in fill_between
    y1 = ma.masked_invalid(self.convert_yunits(y1))

  File "/scratch/s0XXXX/anaconda/lib/python2.7/site-packages/numpy/ma/core.py", line 2388, in masked_invalid
    condition = ~(np.isfinite(a))

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

任何想法如何解决这个问题?

【问题讨论】:

    标签: python python-2.7 python-iris


    【解决方案1】:

    问题在于您将 Iris Cubes 传递到 matlpotlib 函数中。您需要传入 Cube 的 data 数组。

    【讨论】:

    • 谢谢丹尼尔。我已将其更改为plt.fill_between(x,CRUYR_mean.data, UDelYR_mean.data, color='grey', alpha='0.5'),但现在出现ValueError: Argument dimensions are incompatible...图表仍然显示,但没有填充。
    猜你喜欢
    • 2020-11-02
    • 2012-06-07
    • 1970-01-01
    • 2017-09-08
    • 2021-01-06
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多