-1

I'm using matplotlib in Python to create a stacked bar chart showing order volume over the course of the day by hour, versus a calendar equivalent day last year.

I've already arranged an array that includes today's and last year's order volume:

allorders=[(23, 28), (15, 7), (15, 5), (8, 9), (4, 2), (5, 3), (4, 6), (8, 10), (28, 24), (45, 46), (55, 65), (0, 74), (0, 64), (0, 58), (0, 62), (0, 62), (0, 42), (0, 43), (0, 38), (0, 39), (0, 32), (0, 40), (0, 41), (0, 16)]

For stacked bars, you would normally use the following syntax:

import matplotlib.pyplot as plt
import numpy as np
n=2
ind = np.arange(n) 
width = 0.35       
plt.ylabel('Orders')
plt.xticks(ind+width/2., ('Today', 'Last Year on Calendar Equivalent'))
plt.yticks(np.arange(0,plottotal,10))

p1= plt.bar(ind, allorders[0],   width, color='#000099')
p2= plt.bar(ind, allorders[1],   width, color='#000099', bottom=allorders[0])

however, this can be daunting with large amount of stacked charts. Therefore, I'm trying to create a loop to go through the 'allorders' array and stack them via:

 for i in allorders:
     if i=0:
       p1=plt.bar(ind, allorders[i],   width, color='#000099')
       bottomcounter=allorders[i]

     else: 
       'p+i' = plt.bar(ind, allorders[i],   width, color='r', bottom=bottomcounter)
       bottomcounter=bottomcounter+allorders[i]

but, I get all sorts of errors, including that the clause if i=0 has invalid syntax, and that 'p+i'cannot be used to auto-name variables.

So, SO wizards:

  1. how do you name new variables automatically in a loop (so when i=0, the variable is named p0, when i=1, the variable is named p1, etc.)

  2. what's wrong with including the if i=0 clause?

Since I'm new to Python (coming from R and Stata), please treat me like an infant, with step-by-step code if you can!

Thanks!

Zephyr
  • 11,891
  • 53
  • 45
  • 80
user1784454
  • 87
  • 1
  • 2
  • 5
  • 3
    In generally speaking, your idea is right. The problem is that there're too many syntax errors in your code. You have to learn to **DEBUG**, instead of watching the errors with no action but paste your codes here. – Timothy Dec 28 '12 at 16:42
  • Hey Skyler -- I understand the need to debug here, but since I'm new to Python I don't know how to respond to the errors (like the 'i=0' exception). – user1784454 Dec 28 '12 at 16:53
  • 1
    Try `if i==0` instead. It is a very basic syntax of Python. Please get a book and learn Python. SO works for specific problems, but we don't provide any service **before your own try**. – Timothy Dec 28 '12 at 17:09
  • 1
    also, in general dynamically creating named variables is a bad idea, use a [`list`](http://docs.python.org/2/library/functions.html#list) or [`dict`](http://docs.python.org/2/library/stdtypes.html#mapping-types-dict). – tacaswell Dec 28 '12 at 17:23
  • Hey Skyler -- I've already tried if i==0, and still get the invalid syntax error... – user1784454 Dec 28 '12 at 17:25
  • If you insist on hanging your self: http://stackoverflow.com/questions/13096604/creating-variables-in-python/13096669#13096669 – tacaswell Dec 28 '12 at 17:29
  • Did you ever get this sorted out? – tacaswell Oct 05 '13 at 00:50

1 Answers1

0

To start with, you need to read and understand the official tutorial.

Second, to fix your code:

import itertools
allorders=[(23, 28), (15, 7), (15, 5), (8, 9), (4, 2), (5, 3), (4, 6), (8, 10), (28, 24), (45, 46), (55, 65), (0, 74), (0, 64), (0, 58), (0, 62), (0, 62), (0, 42), (0, 43), (0, 38), (0, 39), (0, 32), (0, 40), (0, 41), (0, 16)]
width = .35
plts = []
colors = ['k','r','b']

for i,order in enumerate(allorders):
    bottom_counter = 0
    loc_plts = []
    for o,c in zip(order,itertools.cycle(colors)):
        tmp_plt = plt.bar(i, o, width, color=c, bottom=bottom_counter)
        loc_plts.append(tmp_plt)
        bottom_counter+=o
    plts.append(loc_plts)
tacaswell
  • 84,579
  • 22
  • 210
  • 199