Fill_between gives "ValueError: argument sizes are incompatible"

The code below written in Python imports data from Excel to Python, then displays it with matplotlib. I am trying to fill above and below line 80 with different colors using the fill_between function, but it gives

 ValueError: Argument dimensions are incompatible 

Note. the Excel file ( 'eegg.xlsx' ) has 4 columns with 682 rows and contains int data (0-100).

I think the problem is with the where argument of the fill_between calls, but I can't solve it.

 import xlrd import numpy from datetime import time from pylab import * workbook = xlrd.open_workbook('eegg.xlsx') worksheet = workbook.sheet_by_name('Sayfa1') num_rows = worksheet.nrows - 1 num_cells = worksheet.ncols - 1 curr_row = -1 att=[] med=[] for i in [2,3]: kolon = worksheet.col(i) for x in kolon[1:]: d= int(x.value) if i==2: att.append(d) elif i==3: med.append(d) n = len(att) X = np.linspace(0,n,n,endpoint=True) Y1 = att plot(X, Y1, color='blue', alpha=1.00) fill_between(X, 0, Y1, (Y1) > 80, color='red', alpha=.25) fill_between(X, 0, Y1, (Y1) < 80, color='blue', alpha=.25) xlim(0,n), xticks([]) ylim(0,110), yticks([]) 
+6
source share
3 answers

You get this error because Y1 is a list , not numpy.array , and therefore (Y1) > 80 and (Y1) < 80 return one bool each, not their array, since kwarg where accepts.

So replacing the line

 Y1 = att 

with

 Y1 = array(att) 

should solve the problem.

+11
source

It seems that the arguments color and alpha should be passed as color = ..., alpha = ...

Correct: ax.fill_between (x, y_min, y_max, color = color, alpha = 0.1)

Invalid: ax.fill_between (x, y_min, y_max, color, alpha = 0.1)

+1
source

Be sure to convert X and Y1 to pandas.core.series.Series. This should solve the problem. You can check the type with:

 type(X) type(Y1) 

If both return "pandas.core.series.Series", then it should work.

Just to illustrate, if X, Y is dataframes, try the following:

 X = X.iloc[:,0] # NEW Y = Y.iloc[:,0] # NEW 
0
source

Source: https://habr.com/ru/post/952021/


All Articles