Skip to content

Latest commit

 

History

History

PlotlyExample

Plotly Fundamentals

This section would explain the basics of Plotly and how to use Python to generate simple Plotly charts. We will be focus on plotly.graph_objs (go).

Installation

pip install plotly==version - check Plotly documenation for the latest version
pip install ipython - Plotly relies on ipython to render an interactive graph

Setting and Basics

This tutorial lets you to generate Plotly graphs office.

Required package:

  • plotly.graph_objs (go) - To specify the chart type
  • plotly.offline - Allow you to generate the plot offline



Each Plotly chart requires the following elements:

  • init_notebook_mode(connected=True)
  • data list
  • layout
  • go.Figure() - object to store data list and layout
  • plotly.offline.plot() - generate the chart and save in html

Data list

A data list contain one or multiple plotly.graph_objs, which contains the metadata of visualization type, configuration, and data. For example, you may add a go.Scatter() into a list like below:

data = []
data.append(go.Scatter(x=df['hour'],y=df['amount']))

Plotly objects accepts lists, Pandas DataFrames, or NumPy Arrays.

Layout

Layout is a dictionary consists the detailed configuration of the visualization, such as x-axis, y-axis, legend, background configuration. For example, you may set a title in this dictionary like below:

layout = {'title':{'text':'Number of Customer Trend by Days', 'x':0.5}}

go.Figure()

go.Figure() requires two elements to generate a Plotly graph: Data list and Layout. Once go.Figure() has accepted both elements, you may generate a visualization with plotly.offline.plot()

After you have prepared a data list and a dictionary of layout, you may pass those as agrument into go.Figure():

fig = go.Figure(data=data, layout=layout)

plotly.offline.plot()

The function to trigger Plotly to render the visualization on a HTML page according to go.Figure():

plotly.offline.plot(fig, filename='my_scatterplot.html')

Plotly would save the HTML page in the current directory, but the browser would also automatically render the HTML.

General Layout of a Typical Simple Plotly Chart with Python

import pandas as pd
import plotly
import plotly.graph_objs as go
from plotly.offline import *


# To initiate ploty to run offline
init_notebook_mode(connected=True)
# Import data
df = pd.read_csv('../Data/tips.csv')

# Data
data = []
data.append(go.Scatter(x=df['grand_total'], y=df['tips'],
					mode='markers'))
# Layout
layout = {'title':{'text':'Everybody\'s Tipping Distribution', 'x':0.5}}

fig = go.Figure(data=data, layout=layout)

plotly.offline.plot(fig, filename='simple_scatterplot.html')

Logarithm Plots in Plotly

Creating a logarithm plot is very useful when visualizing very skewed data. Plotly allows you to simply pass one parameter in the layout dictionary to turn a regular plot to a logarithm plot without manually adjust the data points. Add the type parameter equals to log under the desired columns. Here is the example to take logarithm on the observations on both X-axis and Y-axis:

layout = {'title':{'text':'Everybody\'s Tipping Distribution', 'x':0.5},
	'xaxis':{'type':'log'}, 'yaxis':{'type':'log'}}


Reference: Plotly Documentation

Background Colour

The default background colour on Plotly plots are gray, but you may adjust it by adding the plot_bgcolor parameter along with the RGBA colour scale to the layout dictionary. Here is the example to turn the background colour to white:

layout = {'title':{'text':'Everybody\'s Tipping Distribution', 'x':0.5},
	'xaxis':{'gridcolor':'lightgray'}, 'yaxis':{'gridcolor':'lightgray'},
	'plot_bgcolor': 'rgba(0,0,0,0)'}

Note:
  • Because after Plot BG colour is turned to white, it is hard to compare data value with the axis ticks. Therefore, it is recommended to add 'gridcolor':'lightgray' to both X-axis and Y-axis
  • White background and light gray grid colour is the default setting to the Template Framework

Bar Charts

You may find how to create bar charts with Plotly in the Bar Charts folder.

Line Charts

You may find how to create line charts with Plotly in the Line Charts folder.

Scatter Plots/Bubble Charts

You may find how to create scatter plots with Plotly in the Scatter Plots folder.

Box Plots

You may find how to create Box plots with Plotly in the Box Plots folder.

Pie/Donut Charts

You may find how to create Pie charts/Dount charts with Plotly in the Pie Chart folder.

Treemaps

You may find how to create Treemap with Plotly in the Treemap folder.

Histogram

You may find how to create Histogram with Plotly in the Histogram folder.

Heatmaps

You may find how to create Heatmap with Plotly in the Heatmp folder.

Candlestick Charts

You may find how to create candlestick charts with Plotly in the Candlestick Charts folder.

Choropleth Graphs

You may find how to create Choropleth graph with Plotly in the Choropleth Graph folder.

Sunburst Charts

You may find how to create Sunburst Charts with Plotly in the Sunburst Charts folder.

Parallel Coordinates Plot

You may find how to create Parallel Coordinates Plot with Plotly in the Parallel Coordinates Plot folder.

Sankey Chart

You may find how to create Sankey Chart with Plotly in the Sankey Chart folder.

Funnel Chart

You may find how to create Funnel Chart with Plotly in the Funnel Chart folder.