Skip to content Skip to sidebar Skip to footer

Python - Double Bars Are Cut Out Of Chart Area

I am plotting a double bar graph and line graph on the same chart. For the first and last x-tick, only one bar is visible in the chart. All the x-ticks in the middle have double ba

Solution 1:

Change the x limits and both should appear:

plt.xlim(-1,13)

Solution 2:

You need to set the xlimits after creating the plot.

for h in range(0,len(x)):   # len(x) is > 4000
    if h > 0 :          
        fig, ax1 = plt.subplots()   
        ax2 = ax1.twinx()
        df2[['A','B']].plot(kind='bar', ax=ax1, figsize=(15, 10), legend=False)
        df2[['C']].plot(style='r-', ax=ax2, figsize=(15, 10), marker='o',legend=False)

        ax1.set_xlabel("x-label", fontsize=15)
        ax1.set_ylabel("y-label 1", fontsize=15)
        ax2.set_ylabel("y-label 2", fontsize=15)

        ax1.set_xlim((-1,13))  # <-----   here

        plt.tight_layout()
        plt.grid()
        fig.savefig('xyz.png')
        plt.close()

Post a Comment for "Python - Double Bars Are Cut Out Of Chart Area"