Description:
This program plots two different data sets — revenue and units sold — on the same x-axis (products or time), but using two y-axes to handle different scales.
twinx()
creates a second Y-axis on the right side.
Useful when comparing two datasets with different scales.
One axis can show revenue, another can show units sold.
Both plots share the same X-axis (like time or category).
import matplotlib.pyplot as plt
# Sample data
products = ['A', 'B', 'C', 'D', 'E']
revenue = [10000, 15000, 12000, 18000, 16000]
units_sold = [200, 250, 220, 300, 270]
# Create the figure and primary axis
fig, ax1 = plt.subplots(figsize=(8, 5))
# Plot revenue on the primary y-axis
ax1.set_xlabel('Product')
ax1.set_ylabel('Revenue', color='blue')
ax1.plot(products, revenue, color='blue', marker='o', label='Revenue')
ax1.tick_params(axis='y', labelcolor='blue')
# Create secondary y-axis for units sold
ax2 = ax1.twinx()
ax2.set_ylabel('Units Sold', color='green')
ax2.plot(products, units_sold, color='green', marker='s', linestyle='--', label='Units Sold')
ax2.tick_params(axis='y', labelcolor='green')
# Add title and grid
plt.title('Revenue and Units Sold (Dual Y-Axis)')
fig.tight_layout()
plt.grid(True)
plt.show()
Output:
Trainings :
Data Science Training in Indore | Data Analytics Training in Indore | Python Training in Indore | Blockchain Training in Indore | React JS Training in Indore | Web Development Training in Indore | Full Stack Development Training in Indore |Free Courses and Resource :
Verbal Aptitude | Matplotlib Tutorials | Examples | Interview Questions | Pandas Tutorials | Projects | Interview Questions | OpenCV Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Dart | Python Data Structures and Algorithms | Rust Tutorials | Projects | Interview Questions | Aptitude Tests | HackerRank Python | Django | MongoDB Tutorials | Examples | Interview Questions | NumPy Practice Questions | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | React.js Tutorials | Verbal Ability Tutorial | MS Sql Server Tutorials | Examples | Interview Questions | Power BI Tutorials | Projects | Interview Questions | Example Dashboards | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial | HackerRank C++ Solutions | Tensor Flow | Pandas Practice Questions | Python Practice Questions | C++ Tutorials | Quantitative Ability Tutorial | Javascript | Node.js Tutorials | HackerRank SQL Solutions | HackerRank DSA Solutions | Verbal Aptitude 2 | HackerRank Java Solutions | HTML Tutorial | TCS NQT Mock Test Series | Reasoning Ability Tutorial | CodeChef Python Solutions | HackerRank C Program Solutions | Leetcode Python Solutions | SQL Practice Question | Matplotlib Practice Questions |Interview Questions :
Pandas Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |