Instagram
youtube
Facebook
Twitter

Integrate with Plotly for Comparison

Description:

A Python program that uses Plotly to compare sales of multiple products with interactive visualizations.

Code Explanation:

● Install Plotly using pip install plotly before running the code.
plotly.graph_objs is used to define the visual elements like lines and markers.
pd.DataFrame(data) creates a structured dataset with sales over time.
go.Scatter() is used to create interactive line+marker plots for each product.
go.Figure() combines multiple plots into one interactive chart.
update_layout() adds titles, axis labels, and hover options.
plot(fig) renders the chart in your browser with full interactivity.

 

Program:

import pandas as pd
import plotly.graph_objs as go
from plotly.offline import plot

# Sample data
data = {
    'Date': ['2025-01-01', '2025-01-02', '2025-01-03', '2025-01-04'],
    'Laptop': [1500, 1600, 1700, 1800],
    'Tablet': [1200, 1100, 1300, 1250]
}
df = pd.DataFrame(data)

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

# Create traces for each product
trace1 = go.Scatter(x=df['Date'], y=df['Laptop'], mode='lines+markers', name='Laptop')
trace2 = go.Scatter(x=df['Date'], y=df['Tablet'], mode='lines+markers', name='Tablet')

# Combine the traces into a figure
fig = go.Figure(data=[trace1, trace2])

# Customize layout
fig.update_layout(
    title='Interactive Comparison of Product Sales',
    xaxis_title='Date',
    yaxis_title='Sales',
    template='plotly',
    hovermode='x unified'
)

# Show the interactive plot
plot(fig)

 

 

Output: