I extract data out of a database (via Django and Python). I'm drawing two bar charts. One is a stacked bar, and one is a simple bar.
The code for each graph is in their own function. I call the function to draw the graph sequencially (in function "CreateAllWebSite() below).
Problem is that when I do this, the graph called second comes out all jumbled. See attached copies of png files. If I swap the function calls, always first is ok and the the second one is jumbled. If I call them one at a time, they both work just fine (which is how i developed the second). once put together, the second is wrong. For example, the legend from the first graph gets put on the second graph . The x-axis for the second is all jumbled at the bottom left corner of the graph instead of along the x-axis properly.
I'm using Matplotlib 0.98.5.3 on OS X. I will upgrade to 1.0 of Matplot lib soon.
I suspect some sort of memory issue; perhaps caused by how I import matplotlib in the global section of the python program; but far as I can tell this is correct, or is it? I suspect memory as how else would legend from the first graph get put on the second graph? Is there some sort of reset function to call to clear out the old graph? I sort of thought that calling a completely different function would cover that.
Here's the code extract with portions I think no relevant snipped out. From debugging I'm confident the number arrays holding the data is ok. As I said, when producing the graphs one at a time, it work fine. So that code snipped out below.
#!/usr/bin/env python
# −*− coding: UTF−8 −*−
import matplotlib
matplotlib.use('Agg') # before importing pyplot per docs
import matplotlib.pyplot as plt
import numpy as np
[snipped the import of Python and Django libraries]
···
###########################################################
def CreateAllWebSite():
# here is the one function that calls the two graphing functions sequentially.
# whichever of these is called first is done fine. Second flawed. calling one at at time, each works ok
CreateMemberStatFigure()
CreateMemberStatCategoryFigure()
###############################################
def CreateMemberStatFigure():
# this function creates a stacked bar chart
[snipped out portions where arrays created]
corp1=[]
corp2=[]
corp3=[]
ordm=[]
ordr=[]
ordh=[]
month=[]
for i in memberstatsp:
month.append(None2Zero(i['month'])) # None2Zero simply converts any "None" data to a number zero.
corp1.append(None2Zero(i['CORP1']))
corp2.append(None2Zero(i['CORP2']))
corp3.append(None2Zero(i['CORP3']))
ordm.append(None2Zero(i['ORM']))
ordr.append(None2Zero(i['ORMR']))
ordh.append(None2Zero(i['HON']))
N=len(month)
corp1=np.array(corp1)
corp2=np.array(corp2)
corp3=np.array(corp3)
ordm=np.array(ordm)
ordr=np.array(ordr)
ordh=np.array(ordh)
# create new bases for each bar
base=np.zeros(N,int)
base1=base+ordh
base2=base1+corp3
base3=base2+corp2
base4=base3+corp1
base5=base4+ordm
width = 24 # the width of the bars: can also be len(x) sequence
p1 = plt.bar(month, ordh, width, color=barchartcolor[0],linewidth=0)
p2 = plt.bar(month, corp3, width, color=barchartcolor[1],bottom=base1,linewidth=0)
p3 = plt.bar(month, corp2, width, color=barchartcolor[2],bottom=base2,linewidth=0)
p4 = plt.bar(month, corp1, width, color=barchartcolor[3],bottom=base3,linewidth=0)
p5 = plt.bar(month, ordm, width, color=barchartcolor[4],bottom=base4,linewidth=0)
p6 = plt.bar(month, ordr, width, color=barchartcolor[5],bottom=base5,linewidth=0)
plt.yticks(np.arange(0,200,20))
plt.title(ORG_NAME+' Membership by Month')
plt.ylabel('Count')
plt.legend( (p1[0], p2[0], p3[0], p4[0], p5[0], p6[0]),mtypes,loc='best')
plt.show()
matplotlib.rcParams['figure.figsize']=(3,2)
fn=TEMP_DIR+SOC_MEMBER_STAT_BARCHART_FIGURE
plt.savefig(fn)
return
###############################################
def CreateMemberStatCategoryFigure():
membercategories=Membercategory.objects.all() # gets the categories out of the database
mcats=[]
cnt=[]
for i in membercategories:
mcats.append(i.category)
cnt.append(Member.Active_objects.filter(membercategory__category=i.category).count())
xlocations = np.array(range(len(mcats)))+0.5
p1 = plt.bar(xlocations,cnt)
plt.figtext(0.15,0.85,dt)
plt.xticks(xlocations+0.4, mcats,rotation=17)
plt.title(ORG_NAME+' Membership by Member Category')
plt.ylabel('Count')
plt.show()
matplotlib.rcParams['figure.figsize']=(3,2)
fn=TEMP_DIR+SOC_MEMBER_STAT_BARCHART_CAT_FIGURE
plt.savefig(fn)
return
################################################