I am new in python and I did a tkinter interface where questions are asked and then it plots graphics in another window.
I am working on big csv files using panda library.
The problem I have is when, on my graphics windows, I try to plot 3 stacked histograms on the same subplot.
Let me be more specific, I have 3 values, alert values (VA, VI and VR), years values and location values (named PK (type : float)).
I need to plot 3 stacked histogram (by year) next to each other for each location and for each alert value in a subplot in a canvas.
I tried to use matplotlib to plot the three alert values sorted by location but I cannot figure out how to show the years on the graph. So I tried another method but it does not fully work.
I tried to do some df.groupby.plot, it works if I plot just one stacked histogram but not if I want to show the three of them. I succeed in plotting the three differents stacked histogram but it shows only the locations where the three values are and they are not next to each other but are overlapping each other. That's not what I want.
Sometimes I do not have VI or VR that's why I put a try/except so that it does not stop my script.
Can you help me to figure this out?
Here is an exemple of the df I want to plot :
g3m['pk']=[1,1,1,2,2,2,4,4,5,5,5,5,5,5,6,6,10,10,10,12,12,12,12,12,12,12,12]
g3m['annee']=[2010,2011,2012,2010,2012,2013,2011,2014,2010,2011,2012,2012,2012,2016,2016,2017,2010,2010,2014,2010,2010,2010,2010,2012,2013,2014,2014]
g3m['class_seuil']=['VA','VA','VA','VA','VA','VA','VA','VA','VA','VA','VA','VI','VR','VI','VI','VA','VI','VI','VR','VA','VA','VI','VI','VA','VI','VR','VA']
g3m['type_mesure']=['MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC','MAC']
It is a Panda DataFrame.
Now here is my code for plotting in the subplot :
here is the declaration of my figure :
f = Figure(figsize=(15,8), dpi=100)
f.subplots_adjust(top=0.97,hspace=0.41,left=0.05,bottom=0.08,right=0.91)
a = f.add_subplot(311)
c = f.add_subplot(312)
b = f.add_subplot(313)
and Here is my code for the plot :
try:
g3mVa=pa.DataFrame()
g3mVa['pk']=g3m[g3m['class_seuil']=='VA']['pk_decimal_km_m']
g3mVa['annee']=g3m[g3m['class_seuil']=='VA']['annee']
g3mVa['type_mesure']=g3m[g3m['class_seuil']=='VA']['type_mesure']
g3mVa.groupby(['pk','annee']).count().unstack().plot(kind='bar',stacked=True,colormap='Greens',y='type_mesure',grid=True,ax=c,rot='horizontal')
c.hold(True)
except:
pass
try:
g3mVi=pa.DataFrame()
g3mVi['pk']=g3m[g3m['class_seuil']=='VI']['pk_decimal_km']
g3mVi['annee']=g3m[g3m['class_seuil']=='VI']['annee']
g3mVi['type_mesure']=g3m[g3m['class_seuil']=='VI']['type_mesure']
g3mVi.groupby(['pk','annee']).count().unstack().plot(kind='bar',stacked=True,colormap='Oranges',y='type_mesure',grid=True,ax=c,rot='horizontal')
c.hold(True)
except:
pass
try:
g3mVr=pa.DataFrame()
g3mVr['pk']=g3m[g3m['class_seuil']=='VR']['pk_decimal_km_p']
g3mVr['annee']=g3m[g3m['class_seuil']=='VR']['annee']
g3mVr['type_mesure']=g3m[g3m['class_seuil']=='VR']['type_mesure']
g3mVr.groupby(['pk','annee']).count().unstack().plot(kind='bar',stacked=True,y='type_mesure',colormap='Reds',grid=True,ax=c,rot='horizontal')
c.hold(True)
except:
pass
Can you help me?
Is it possible to plot it like I want?