Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fill_between gives "ValueError: Argument dimensions are incompatible"

Code below written in Python imports data from Excel to Python, then plots with matplotlib. I'm 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 cannot solve this.

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([])
like image 650
acs Avatar asked Aug 19 '13 11:08

acs


3 Answers

Make sure to convert X and Y1 to pandas.core.series.Series. That 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 for illustration, if X, Y are dataframes, then try the following:

X = X.iloc[:,0]  # NEW
Y = Y.iloc[:,0]  # NEW
like image 63
Lothar Avatar answered Oct 29 '22 22:10

Lothar


You get this error because Y1 is a list, not an numpy.array, and therefore (Y1) > 80 and (Y1) < 80 return a single bool each, not an array of them, as the kwarg where accepts.

So replacing the line

Y1 = att

with

Y1 = array(att)

should solve the problem.

like image 39
sodd Avatar answered Oct 29 '22 21:10

sodd


It seems that arguments color and alpha need to be passed as color =...,alpha=...

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

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

like image 2
Martin Alexandersson Avatar answered Oct 29 '22 21:10

Martin Alexandersson