Plotly Python Tutorial: Scatter, Bar, Histogram, Express, 3D, and Density Map Visualizations
This tutorial demonstrates how to install Plotly, import necessary modules, and create various visualizations—including scatter, bar, histogram, pie, 3D, and density map charts—using both the graph_objects and express APIs with sample CSV data and code snippets.
First, install Plotly via pip.
pip install plotlyImport the required libraries: plotly.graph_objects for low‑level chart construction, plotly.offline for offline rendering, and pandas for data handling.
import plotly.graph_objects as go
import plotly.offline as of # offline mode
import pandas as pdExample 1 – Scatter Plot : Load a New Zealand weather CSV, create two go.Scatter traces for Auckland and Wellington, set chart titles and axis labels, and display the figure with of.plot(fig) .
data = pd.read_csv(r'C:\Users\Administrator\Desktop\data\nz_weather.csv')
line1 = go.Scatter(y=data['Auckland'], x=data['DATE'], name='Auckland')
line2 = go.Scatter(y=data['Wellington'], x=data['DATE'], name='Wellington')
fig = go.Figure([line1, line2])
fig.update_layout(title='New Zealand Weather',
xaxis_title='DATE',
yaxis_title='Weather')
of.plot(fig)Example 2 – Bar Chart : Filter the data for the year 2010, create two go.Bar traces, add text labels positioned outside the bars, and render the chart.
data_2010 = data[(data['DATE'] >= '2010-01') & (data['DATE'] < '2011-01')]
bar1 = go.Bar(y=data_2010['Auckland'], x=data_2010['DATE'],
text=data_2010['Auckland'], textposition='outside', name='Auckland')
bar2 = go.Bar(y=data_2010['Wellington'], x=data_2010['DATE'],
text=data_2010['Wellington'], textposition='outside', name='Wellington')
fig = go.Figure([bar1, bar2])
fig.update_layout(title='New Zealand Weather',
xaxis_title='DATE',
yaxis_title='Weather')
of.plot(fig)Example 3 – Histogram : Use go.Histogram to count occurrences of weather values for Auckland and Wellington, adjust bin size, and display the result.
hist1 = go.Histogram(x=data['Auckland'], xbins={'size': 10}, name='Auckland')
hist2 = go.Histogram(x=data['Wellington'], xbins={'size': 10}, name='Wellington')
fig = go.Figure([hist1, hist2])
fig.update_layout(title='New Zealand Weather',
xaxis_title='Weather',
yaxis_title='count',
bargap=0.1)
of.plot(fig)Example 4 – Plotly Express (Scatter) : Load the Iris dataset and create a scatter plot with color encoding using px.scatter , then render it.
import plotly.express as px
data = pd.read_csv(r'C:\Users\Administrator\Desktop\data\iris.csv')
fig = px.scatter(data, x='SepalLength', y='SepalWidth', color='Name')
of.plot(fig)Example 5 – Pie Chart : Read a CSV containing label/value pairs and generate a pie chart with px.pie .
data = pd.read_csv(r'C:\Users\Administrator\Desktop\data\pie-charts-with-excel.csv')
fig = px.pie(data, names=data['Label'], values=data['Values'], title='pie-charts-with-excel')
of.plot(fig)Example 6 – 3D Scatter Plot : Show two ways to create a 3D scatter plot—first with go.Scatter3d , then with px.scatter_3d .
# Using graph_objects
line1 = go.Scatter3d(x=data1['x'], y=data1['y'], z=data1['z'],
mode='markers', marker={'size': 3, 'color': data1['color']})
fig4 = go.Figure(line1)
of.plot(fig4)
# Using plotly.express
fig5 = px.scatter_3d(data1, x='x', y='y', z='z', color='color')
of.plot(fig5)Example 7 – Density Mapbox : Load earthquake data, create a density map with go.Densitymapbox , set the map style, and display it.
data = pd.read_csv(r'C:\Users\Administrator\Desktop\data\earthquakes.csv')
my_map = go.Densitymapbox(lat=data['Latitude'], lon=data['Longitude'],
z=data['Magnitude'], radius=4)
fig = go.Figure(my_map)
fig.update_layout(mapbox_style='open-street-map')
of.plot(fig)Each code block can be executed after installing the required packages, and the resulting figures are displayed inline using Plotly's offline mode.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.