Instagram
youtube
Facebook
Twitter

Change the Line Style in a Plot

Description:
This program creates a line chart of revenue over time with a dashed line style and data points clearly marked.

Code Explanation:
● Used the same revenue-by-date data from earlier.

● Used plt.plot() to draw a line chart of revenue over time.

● Set linestyle='--' to make the line appear dashed.

● Added marker='o' to show data points clearly.

● Colored the line blue and kept the layout clean with labels and grid.

 

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)

# Convert 'Date' column to datetime
df['Date'] = pd.to_datetime(df['Date'])

# Calculate Revenue
df['Revenue'] = df['Quantity'] * df['Price']

# Group by Date
revenue_by_date = df.groupby('Date')['Revenue'].sum()

# Plotting with custom line style (dashed line)
plt.figure(figsize=(8, 5))
plt.plot(revenue_by_date.index, revenue_by_date.values, color='blue', linestyle='--', marker='o')
plt.title('Revenue Over Time (Dashed Line)')
plt.xlabel('Date')
plt.ylabel('Revenue')
plt.grid(True)
plt.tight_layout()
plt.show()

 

Output: