Skip to content Skip to sidebar Skip to footer

Python Plot Bar Chart And Percentage Line Chart On Same Graph

I am trying to plot one or more lines on the same chart as a bar chart to show a few different metrics. I heard I should use ax.twinx() for this, but I either get an error saying x

Solution 1:

Try this:

fig, ax = plt.subplots()
ax2 = ax.twinx()


df4[['Date','Qty']].set_index('Date') \
                   .plot(kind='bar',
                         ax=ax,
                         color = 'dodgerblue',
                         figsize=(13,4),
                         legend=False)

patches, labels = ax.get_legend_handles_labels()
ax.legend(patches, labels, loc='upper left')

ax.set_xlabel(r"$\rm \bf{Date}$", fontsize=18, rotation=0)
ax.set_ylabel(r'$\cal \bf{Qty}$ ',fontsize=18, rotation=90)

ax2.plot(range(len(df4)), df4['Rate'], 'green', label='Rate',
         linestyle = '--', linewidth=2.0)

patches, labels = ax2.get_legend_handles_labels()
ax2.legend(patches, labels, loc='upper right')

NOTE: if you want a tested solution, please provide a sample data

Post a Comment for "Python Plot Bar Chart And Percentage Line Chart On Same Graph"