Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overcome ValueError for empty array

In this discussion I tried to fix an issue in plotting limits for y-axis, after the twiny() messes up my plot.

I thought this:

ax.set_ylim([y.min()-0.05, y.max()+0.05]) 

was a good solution. And probably it is, for continuous set of data. As I said in that discussion, anyway, my data are noisy, and sometimes with gaps. So it happens that some plotted ranges have no data. In that case, naturally, the use of the .min() raises the error:

ValueError: zero-size array to reduction operation minimum which has no identity 

because the array is empty. How to work around it, so that the code just does not care about putting limits on the y-axis? (Hoping that this is the only issue the empty array will cause)

like image 734
Py-ser Avatar asked Apr 07 '14 03:04

Py-ser


1 Answers

Just catch the exception and ignore it:

try:     ax.set_ylim([y.min()-0.05, y.max()+0.05]) except ValueError:  #raised if `y` is empty.     pass 
like image 135
mgilson Avatar answered Oct 09 '22 09:10

mgilson