Issue
I would like to fix the gap between 2012 and 2013 in the below bar plot.
My dataframe is
In [30]: df
Out[30]:
Pre-Release Post-Release
FinishDate
2008 1.0 0.0
2009 18.0 0.0
2010 96.0 0.0
2011 161.0 0.0
2012 157.0 0.0
2013 0.0 139.0
2014 0.0 155.0
2015 0.0 150.0
2016 0.0 91.0
2017 0.0 15.0
and I'm using df.plot(kind='bar', width=1)
to plot.
Solution
There is no actual "Gap" in your graph: Pandas is just reserving space to plot two different bars next to each other. Take this piece of code to illustrate:
from io import StringIO
import pandas as pd
TESTDATA=StringIO("""2008 1.0 0.0
2009 18.0 5.0
2010 96.0 0.0
2011 161.0 0.0
2012 157.0 0.0
2013 0.0 139.0
2014 0.0 155.0
2015 0.0 150.0
2016 0.0 91.0
2017 0.0 15.0""")
df=pd.read_csv(TESTDATA,delim_whitespace=True,index_col=0)
df.plot(kind='bar')
But you don't actually ever need to print two bars next to each other, so rather than plotting the dataframe, you can plot two series into the same figure:
ax=df['1.0'].plot(kind='bar')
df['0.0'].plot(kind='bar',ax=ax,color='orange')
Alternatively just use:
df.plot(kind='bar', stacked=True)
Which gives you the same results in this case.
Answered By - Sebastian Wozny
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.