1

I created a stacked bar chart via Pandas and the first and last bars are on the edges of the plot. They are cut off.

The plot I want to get is a stacked bar chart and a line chart with 2 y axis. The values of the bar layers should be displayed in the layer.

I created a pd.DataFrame which contains the following:

       1.0  1.3  1.7  2.0   2.3   2.7   3.0  3.3  3.7   4.0   5.0
SS 15  2.0  0.0  1.0  1.0   4.0   8.0   5.0  6.0  6.0  10.0  19.0
SS 16  1.0  1.0  5.0  2.0  11.0  16.0  11.0  8.0  7.0   6.0  12.0
SS 17  1.0  5.0  3.0  3.0   5.0   6.0   5.0  9.0  8.0   9.0  13.0

Every row (SS 15-SS 17) should be one stacked bar, where the value is one layer of the bar. The line chart just contains 3 points which should be connected with a line.

The code I use to plot is:

fig, ax1 = plt.subplots()
ax1 = df.plot(kind='bar',
              stacked=True,
              figsize=(20,15), 
              fontsize=20, 
              grid=True, 
              rot=45, 
              align='center', 
              edgecolor='#000000', 
              colormap='Paired')
ax2 = ax1.twinx()
ax1.set_xlabel('X-LABEL', fontsize=25)
ax1.set_ylabel('1st Y-LABLE', fontsize=25)
ax1.set_title(name, fontsize=35)
ax1.legend(fontsize=15, loc='upper left')
ax1.grid(True, axis='y')
df2 = pd.DataFrame(medi) #medi is just a dict with 3 pais
df2.plot(ax=ax2, 
         color='red', 
         marker='o',
         linewidth='2', 
         fontsize=20)
ax2.legend(["2nd LEGEND"], fontsize=15, loc='upper right')
ax2.set_ylim(1,5)
ax2.set_ylabel('2nd Y-LABEL', fontsize=25)
ax2.grid(True,axis='y')

This results in:

enter image description here

As you can see the first and last bar are right on the edges of the graph and cut off. I tried to override the xticks but it didn't work.

Second problem that I have is to display the values in the bars. I found a solution here, but it results in an error:

AttributeError: 'NoneType' object has no attribute 'update'
Matt Hall
  • 7,614
  • 1
  • 23
  • 36
Holger P.
  • 43
  • 6

1 Answers1

0

I found a solution.

I created the stacked bar via matplotlib and it worked perfect.

I have no idea why the graph got cut off with pandas but it works.

Holger P.
  • 43
  • 6