I would like to make use of the stacked bar plot feature of pandas DataFrames. However, I would like to follow up by modifying the bars which were just plotted. For that, I need access to their graphics handles. But all I get back from df.plot() is an axis handle. How do I get a list of what was specifically created in a particular call? This would be necessary, for instance, if there were preexisting objects in the same axes from an earlier plot, and I wanted to differentiate, ie to treat just the new ones. Here is my code which plots twice to the same axis. I would like to end up having the stacked bars clustered next to each other, so I thought I could just make them a little narrower and shift them right and left. But for that I would need handles of what was made in the second call.
import random
import pandas as pd
import numpy as np
from pylab import *
close('all')
df = pd.DataFrame(np.random.random((7, 5)) * 10,
index=list('abcdefg'), columns=list('ABCDE'))
print df
ax = df.plot(kind='bar', stacked=True, align='center')
c1=ax.containers
hh=findobj(ax,lambda xx:hasattr(xx,'width'))
setp(hh,'width',.3)
print hh
df2= pd.DataFrame(np.random.random((6, 5)) * 10,
index=list('abcdef'), columns=list('FGHIJ'))
ax2 = df2.plot(kind='bar', stacked=True, align='center', ax=ax,colormap='bone')
c2=ax2.containers
print ax==ax2 # Should be true
c1==c2 # Should be False
show()
I have tried to get the list of containers ax.containers after the first call, and then again after the second, and taking the set difference, but this didn't work.