i want produce barchart 2 categories on x axis and, each category, 5 different series. took inspiration here, , amended code follows:
import numpy np import matplotlib.pyplot plt pylab import * %pylab inline myarray=np.array([['series1', 'a',45], ['series2', 'a',47], ['series3', 'a',48], ['series4','a',48], ['series5', 'a',49], ['series6','b',39], ['series7','b',37], ['series8','b',38], ['series9','b',36], ['series10','b',38]]) fig1=plt.figure() ax1=fig1.add_subplot(111) space=0.25 slots=np.unique(myarray[:,0]) categories=np.unique(myarray[:,1]) n=len(slots) width = (1 - space) / (len(slots)) i,cond in enumerate(slots): print "cond:", cond vals = myarray[myarray[:,0] == cond][:,2] pos = [j - (1 - space) / 2. + * width j in range(1,len(categories)+1)] ax1.bar(pos, vals, width=width,label=cond,color=cm.accent(float(i)/n))
i keep getting same error: valueerror: incompatible sizes: argument 'height' must length 2 or scalar
.
it points at: ax1.bar(pos, vals, width=width,label=cond,color=cm.accent(float(i)/n))
.
i understand problem vals
because should either scalar or have length 2, don't know how solve it. elements of vals
float!
import numpy np import matplotlib.pyplot plt pylab import * myarray=np.array([['series1', 'a',45], ['series2', 'a',47], ['series3', 'a',48], ['series4','a',48], ['series5', 'a',49], ['series1','b',39], ['series2','b',37], ['series3','b',38], ['series4','b',36], ['series5','b',38]]) fig1=plt.figure() ax1=fig1.add_subplot(111) space=0.25 slots=np.unique(myarray[:,0]) categories=np.unique(myarray[:,1]) n=len(slots) width = (1 - space) / (len(slots)) i,cond in enumerate(slots[::-1]): print "cond:", cond vals = myarray[myarray[:,0] == cond][:,2] pos = [j - (1 - space) / 2. + * width j in range(1,len(categories)+1)] print(float(i)/n) ax1.bar(pos, vals, width=width,label=cond,color=cm.accent(1-float(i+1)/n)) plt.show()
Comments
Post a Comment