Fundamentals 6 min read

Using Plotly in Python to Create Line, Scatter, and Bar Charts

This tutorial introduces Plotly for Python, showing how to install it, configure offline mode in Jupyter, and create line, scatter, and bar charts with complete code examples and explanations of each visualization type.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using Plotly in Python to Create Line, Scatter, and Bar Charts

Data analysis often requires visualization, and while pandas, matplotlib, pyecharts, and Tableau are common, this article explores Plotly as an alternative for creating high‑quality interactive charts in Python.

Plotly is an online platform that supports many chart types (bar, scatter, pie, histogram, etc.) and multiple languages (Python, JavaScript, MATLAB, R). It can be installed with pip install plotly and works best in Jupyter notebooks, where the offline mode renders charts directly.

First, the necessary libraries are imported and offline mode is initialized:

from plotly.graph_objs import Scatter, Layout
import plotly
import plotly.offline as py
import numpy as np
import plotly.graph_objs as go
# setting offline mode
plotly.offline.init_notebook_mode(connected=True)

The article then demonstrates three chart types.

1. Line Chart – Random x values and three y series are generated, each plotted with different modes (markers, lines+markers, lines). The traces are combined into a list and displayed with py.iplot(data) .

N = 100
random_x = np.linspace(0,1,N)
random_y0 = np.random.randn(N)+5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N)-5
# Create traces
trace0 = go.Scatter(x=random_x, y=random_y0, mode='markers', name='markers')
trace1 = go.Scatter(x=random_x, y=random_y1, mode='lines+markers', name='lines+markers')
trace2 = go.Scatter(x=random_x, y=random_y2, mode='lines', name='lines')
data = [trace0, trace1, trace2]
py.iplot(data)

The resulting line chart shows three distinct visual styles with automatically chosen colors.

2. Scatter Chart – A single trace with random y values is plotted as markers, customizing marker size, color scale, and showing a color legend.

trace1 = go.Scatter(
    y = np.random.randn(500),
    mode = 'markers',
    marker = dict(
        size = 16,
        color = np.random.randn(500),
        colorscale = 'Viridis',
        showscale = True
    )
)
data = [trace1]
py.iplot(data)

This produces a colorful scatter plot where each point’s color reflects its value.

3. Bar (Histogram) Chart – Two bar traces representing monthly data for two products are created and displayed together.

trace0 = go.Bar(
    x = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
    y = [20,14,25,16,18,22,19,15,12,16,14,17],
    name = 'Primary Product',
    marker=dict(color='rgb(49,130,189)')
)
trace1 = go.Bar(
    x = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
    y = [19,14,22,14,16,19,15,14,10,12,12,16],
    name = 'Secondary Product',
    marker=dict(color='rgb(204,204,204)')
)
data = [trace0, trace1]
py.iplot(data)

The bar chart compares two product series across months, illustrating differences in values.

These examples cover only the basics of Plotly; the library also offers many advanced visualizations such as candlestick charts for financial data, especially when combined with pandas.

pythondata visualizationJupyterPlotlyCharting
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.