Description:
A Python program that uses Plotly to compare sales of multiple products with interactive visualizations.
● 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:
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 | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |