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([])
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
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With