Search code examples
python-2.7matplotlibxlrd

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([])

Solution

  • 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.