Tauseef Hussain
Tauseef Hussain

Reputation: 1079

Matplotlib: unsupported operand type(s) for -: 'list' and 'float'

I am trying to create a multi bar graph using matplotlib.

from sqlalchemy import create_engine
import _mssql
from matplotlib import pyplot as plt
import numpy as np


engine = create_engine('mssql+pymssql://sa:[email protected]:1433/AffectV_Test')
connection = engine.connect()
result = connection.execute('SELECT pa.[Date],pa.[Type],(CAST((SUM(pa.Actions_Logged)/SUM(pi.Impressions_Served))*100 AS Decimal(4,4))) AS Daily_Action_Rate from Performance_Actions pa INNER JOIN Performance_Impressions pi ON pa.Alternative = pi.Alternative AND pa.[Date] = pi.[Date] GROUP BY [Type],pa.[Date] ORDER BY pa.[Date] ASC')

click = []
conversion = []
landing = []
xTickMarks = []

for row in result:
    if row[1] == 'click':
        click.append(str(row[2]))
    elif row[1] == 'conversion':
        conversion.append(str(row[2]))
    else:
        landing.append(str(row[2]))
    xTickMarks.append(str(row[0]))



connection.close()

ax = plt.subplot(111)
w = 0.3
ax.bar(xTickMarks-w, click,width=w,color='b',align='center')
ax.bar(xTickMarks, conversion,width=w,color='g',align='center')
ax.bar(xTickMarks+w, landing,width=w,color='r',align='center')
ax.xaxis_date()
ax.autoscale(tight=True)

plt.show()

I get the below error when i try and run the code:

TypeError                                 Traceback (most recent call last)
<ipython-input-63-8a9e652a1846> in <module>()
     32 ax = plt.subplot(111)
     33 w = 0.3
     ---> 34 ax.bar(xTickMarks-w, click,width=w,color='b',align='center')
     35 ax.bar(xTickMarks, conversion,width=w,color='g',align='center')
     36 ax.bar(xTickMarks+w, landing,width=w,color='r',align='center')

TypeError: unsupported operand type(s) for -: 'list' and 'float'

Tried converting the lists to numpy arrays, no go.

The result data set for the above code is:

01/04/2015  click       0.0036
01/04/2015  conversion  0.0010
01/04/2015  landing     0.0058
02/04/2015  click       0.0132
02/04/2015  conversion  0.0014
02/04/2015  landing     0.0104

Referring to the question Question

Upvotes: 0

Views: 5682

Answers (3)

bugdebug
bugdebug

Reputation: 23

For ax.bar(xTickMarks-w, click,width=w,color='b',align='center') this to work.

You may try like this:

index = np.arrange(len(xTickMarks))
ax = plt.subplot(111)

plt.xticks(index, xTickMarks, fontsize = 9)


w = 0.3
ax.bar(index-w, click,width=w,color='b',align='center')
ax.bar(index, conversion,width=w,color='g',align='center')
ax.bar(index+w, landing,width=w,color='r',align='center')

This should work.

Upvotes: 0

plonser
plonser

Reputation: 3363

If you have strings in your xTickMarks I think you want to do this

ax.bar([float(x)-w for x in xTickMarks], ...)

In case that xTickMarks consists of floats you can also do

ax.bar([x-w for x in xTickMarks], ...)

or

xTickMarks = np.asarray(xTickMarks)
ax.bar(xTickMarks-w, ...)

Upvotes: 2

SiHa
SiHa

Reputation: 8411

xTickMarks is a list.

xTickMarks-w raises the exception as you are trying to subtract a float (w = 0.3) from a list.

If you are trying to subtract 0.3 from every element of xTickMarks, then you should use plonsers approach.

Upvotes: 1

Related Questions