Instagram
youtube
Facebook
Twitter

Visualize Total Revenue by Product Using a Bar Plot

Short Description:
This program visualizes total revenue per product using a bar chart based on grouped sales data.

Code Explanation:

● Created a DataFrame using product sales data.

● Calculated a new column 'Revenue' by multiplying Quantity and Price.

● Grouped the data by 'Product' and summed the revenue for each product.

● Used plt.bar() to draw a bar chart of total revenue per product.

● Set the title and axis labels to explain what the chart shows.

● Used tight_layout() to ensure everything fits neatly in the plot area.

 

Program:

import matplotlib.pyplot as plt
import pandas as pd

# Sample data
data = {
    'OrderID': [101, 102, 103, 104],
    'Product': ['Laptop', 'Tablet', 'Smartphone', 'Headphones'],
    'Quantity': [2, 5, 3, 10],
    'Price': [750, 300, 500, 50],
    'Date': ['2025-01-01', '2025-01-01', '2025-01-02', '2025-01-02']
}

# Create DataFrame
df = pd.DataFrame(data)

# Calculate total revenue per order
df['Revenue'] = df['Quantity'] * df['Price']

# Group by Product and sum revenue
revenue_by_product = df.groupby('Product')['Revenue'].sum()

# Plotting the bar chart
plt.figure(figsize=(8, 5))
plt.bar(revenue_by_product.index, revenue_by_product.values, color='orange')
plt.title('Total Revenue by Product')
plt.xlabel('Product')
plt.ylabel('Total Revenue')
plt.tight_layout()
plt.show()

Output: