Language: Python
Data Science
Plotly was created by Chris Parmer, Jack Parmer, and Alex Johnson in 2013 to provide a framework for interactive, web-ready visualizations. It integrates seamlessly with Python, R, MATLAB, and JavaScript, and allows users to create interactive plots for analysis, reporting, and dashboards.
Plotly is a Python graphing library that makes interactive, publication-quality graphs online. It supports a wide variety of chart types, including line plots, scatter plots, bar charts, 3D plots, maps, and dashboards.
pip install plotlyconda install -c plotly plotlyPlotly allows the creation of interactive visualizations with Python. You can generate figures using Plotly Express for simple charts or use the lower-level Graph Objects API for more complex and customized visualizations.
import plotly.express as px
import pandas as pd
df = pd.DataFrame({'x':[1,2,3,4], 'y':[10,15,13,17]})
fig = px.line(df, x='x', y='y', title='Line Plot')
fig.show()Creates an interactive line plot from a Pandas DataFrame and displays it in a browser or notebook.
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x='sepal_width', y='sepal_length', color='species')
fig.show()Generates a scatter plot with color-coded species categories using Plotly Express.
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1,2,3], y=[4,5,6], mode='lines+markers', name='Line 1'))
fig.update_layout(title='Custom Layout', xaxis_title='X Axis', yaxis_title='Y Axis')
fig.show()Demonstrates using Graph Objects to create a scatter plot with custom layout titles and styling.
import plotly.graph_objects as go
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])
fig.show()Creates an interactive 3D surface plot of a mathematical function.
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Bar(x=[1,2,3], y=[2,5,3]), row=1, col=1)
fig.add_trace(go.Scatter(x=[1,2,3], y=[5,3,6]), row=1, col=2)
fig.show()Shows how to create multiple plots in a single figure using subplots with different trace types.
import plotly.express as px
df = px.data.gapminder()
fig = px.scatter(df.query('year==2007'), x='gdpPercap', y='lifeExp', size='pop', color='continent', hover_name='country', size_max=60, animation_frame='year')
fig.show()Creates an animated scatter plot showing changes over years using Plotly Express.
Use Plotly Express for quick, simple visualizations; use Graph Objects for detailed, customized figures.
Leverage hover labels, annotations, and legends for better interactivity.
Combine subplots to display multiple visualizations in one figure.
Use color scales and consistent styling for clarity and accessibility.
Export figures to HTML for sharing interactive visualizations easily.