Add Secondary Axis to Matplotlib Plot
Python
Here we add a secondary axis to a plot for the ax2 object.
1| #Secondary Axis 2| #https://matplotlib.org/gallery/api/two_scales.html 3| 4| fig, ax = plt.subplots(figsize=(12,7)) 5| 6| #Plot net sales value by week data on ax 7| ax.plot(sales_by_week['Week'], sales_by_week['Net_Sale_Value'], label='Net Sales', color='blue') 8| 9| #Create second axes object ax2 and set to share the x axis with ax object 10| ax2 = ax.twinx() 11| 12| #Plot profit by week on second object 13| ax2.plot(sales_by_week['Week'], sales_by_week['Profit'], label='Profit', color='red') 14| 15| #Label axes 16| ax.set_xlabel('Week') 17| ax.set_ylabel('Net Sales') 18| ax2.set_ylabel('Profit') #Label secondary axis 19| 20| plt.show()
133
121
117
109