A dual-axis chart allows plotting two datasets with different scales on the same chart —
one on the primary Y-axis, and the other on a secondary Y-axis.
Example – Student height and weight:
Code: Select all
import pandas as pd
import matplotlib.pyplot as plt
dataset = pd.DataFrame({
'Name': ['Rohit', 'Seema', 'Meena', 'Geeta', 'Rajat'],
'Height': [155, 129, 138, 164, 145],
'Weight': [60, 40, 45, 55, 60]
})
# Primary Y-axis plot (Height)
ax = dataset.plot(kind='line', x='Name', y='Height', color='Blue', linewidth=3)
# Secondary Y-axis plot (Weight)
ax2 = dataset.plot(kind='line', x='Name', y='Weight', secondary_y=True,
color='Red', linewidth=3, ax=ax)
plt.title("Student Data")
# Axis labels
ax.set_xlabel('Name', color='g')
ax.set_ylabel('Height', color='b')
ax2.set_ylabel('Weight', color='r')
plt.tight_layout()
plt.show()