Fundamentals 7 min read

Using Plotly for Data Visualization in Python: Line, Scatter, and Bar Charts

This article introduces Plotly, a powerful Python library for creating interactive line, scatter, and bar charts, explains how to install and use it in offline mode within Jupyter notebooks, and provides complete code examples for each chart type along with visual results.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using Plotly for Data Visualization in Python: Line, Scatter, and Bar Charts

Data analysis relies heavily on visualization, and while tools like Pandas, Matplotlib, Pyecharts, and Tableau are common, this article explores Plotly as an alternative for creating high‑quality interactive charts.

Plotly is an online platform for data analysis and visualization that can generate bar charts, scatter plots, pie charts, histograms, and more. It supports multiple languages (Python, JavaScript, MATLAB, R) via APIs and offers both online and offline modes; the offline mode can be used without an account directly in a Jupyter notebook after installing the package with pip install plotly .

Importing libraries

<code>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)</code>

The code imports the necessary modules and initializes Plotly in offline mode so that charts render inside the notebook.

1. Creating a line chart

<code>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)</code>

This example generates three different series (markers, lines+markers, and lines) and displays them together. The default color scheme is aesthetically pleasing.

2. Creating a scatter plot

<code>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)</code>

Setting mode='markers' produces a scatter plot; the marker dictionary controls point size, color range, color scale, and whether a legend is shown.

3. Creating a bar (histogram) chart

<code>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)</code>

The bar chart compares two product series across months, illustrating how Plotly’s syntax resembles that of Pandas for creating histograms.

These examples only scratch the surface of Plotly’s capabilities; the library can produce many more sophisticated visualizations, such as financial K‑line charts, especially when combined with Pandas.

Plotly chart example

Line chart example

Scatter plot example

Bar chart example

For readers interested in deeper learning, a free Python public course and hundreds of gigabytes of curated learning material (e‑books, tutorials, project templates, source code, etc.) are available via the QR code below.

Recommended reading links are provided at the end of the original article.

Data VisualizationPlotlyChartingJupyter Notebook
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.