DataScience Workbook / 08. Data Visualization / 2. Introduction to Scientific Graphing / 2.2 Plotly-Dash – Data Processing & Interactive Plotting with Python / 2.2.1 Introduction to Plotly (Python library)


Plotly Graphing Library for Python

In this article you will learn the basics about Plotly Graphing Library for Python,
Don’t hesitate to explore the user-friendly documentation:

Plotly installation

Getting started with the Plotly library as a Python developer is straightforward, regardless of the operating system you are working on. Just open a terminal window (or command prompt) and install Plotly with pip or conda following the commands below.

If you don't have pip or Conda installed, start with the tutorial(s) that will guide you through this necessary step:
PIP INSTALLATION, Introduction to Python Programming

Install with pip

In terminal window, execute the command:

pip install plotly==5.8.1

Install with conda

In terminal window, execute the command:

conda install -c plotly plotly=5.8.1
For the efficient development of interactive Python applications, you will also need other libraries, such as Dash, Pandas, or SciPy. So, it is a good idea to create a new virtual environment with Conda right away and add into it other modules over time.

Quick guide (execute commands in the terminal window):

conda info --envs            # Active environment shown with *; on Mac Pro activate base for Miniforge3_x86
conda create -n plotly python=3.8     # replace 'plotly' with custom name
conda activate plotly
pip install plotly==5.8.1

Plotly import

A. Coding in the Terminal
In the terminal window navigate to the desired location and create the empty script file, e.g.


Open the file in the selected text editor (Atom, Nano, Vim, mcedit, etc.) and go to section C of this pharagraph.

B. Coding in the JupyterLab
In the terminal window execute the command provided below to start a new JupyterLab session.

jupyter lab
If you don't have a Jupyter installed, start with the tutorial that will guide you through this necessary step:
JUPYTER INSTALLATION, JupyterLab Development Environment

Once you have launched the Jupyter Development Environment in a browser window, navigate to the desired location in the file system and open a new file under the Python kernel.

C. Import Plotly and copy paste the folowing code snippet:

# -*- coding: utf-8 -*-

import plotly

Plotly Express wrapper

Plotly Express is a built-in module in the Plotly library and provides a high-level API for smart & efficient creation of interactive graphs.

import as px

Color Variables

Color variables in Plotly have a string type value provided in the one of the following forms:

  • HEX color, e.g., “#FFFFFF”
  • HSL color, e.g., “hsl(hue, saturation,lightness)”; The Hue values are in degrees, from 0 to 360 (0 - red, 120 - green, 240 - blue). The saturation and lightness are in % from 0% to 100%.
  • RGB color, e.g., “rgb(red, green, blue)”; Each channel is provided as an intiger between 0 and 255, e.g., rgb(128, 0, 128).
  • RGBA color, e.g., “rgba(r, g, b, a)” with ‘a’ value set up in range from 0.0 (transparent) to 1.0 (solid)
  • common name, e.g., “blue”
Built-in Plotly colors (common names)

aliceblue, antiquewhite, aqua, aquamarine, azure, beige, bisque, black, blanchedalmond, blue, blueviolet, brown, burlywood, cadetblue, chartreuse, chocolate, coral, cornflowerblue, cornsilk, crimson, cyan, darkblue, darkcyan, darkgoldenrod, darkgray, darkgrey, darkgreen, darkkhaki, darkmagenta, darkolivegreen, darkorange, darkorchid, darkred, darksalmon, darkseagreen, darkslateblue, darkslategray, darkslategrey, darkturquoise, darkviolet, deeppink, deepskyblue, dimgray, dimgrey, dodgerblue, firebrick, floralwhite, forestgreen, fuchsia, gainsboro, ghostwhite, gold, goldenrod, gray, grey, green, greenyellow, honeydew, hotpink, indianred, indigo, ivory, khaki, lavender, lavenderblush, lawngreen, lemonchiffon, lightblue, lightcoral, lightcyan, lightgoldenrodyellow, lightgray, lightgrey, lightgreen, lightpink, lightsalmon, lightseagreen, lightskyblue, lightslategray, lightslategrey, lightsteelblue, lightyellow, lime, limegreen, linen, magenta, maroon, mediumaquamarine, ediumblue, mediumorchid, mediumpurple, mediumseagreen, mediumslateblue, mediumspringgreen, mediumturquoise, mediumvioletred, midnightblue, mintcream, mistyrose, moccasin, navajowhite, navy, oldlace, olive, olivedrab, orange, orangered, orchid, palegoldenrod, palegreen, paleturquoise, palevioletred, papayawhip, peachpuff, peru, pink, plum, powderblue, purple, red, rosybrown, royalblue, rebeccapurple, saddlebrown, salmon, sandybrown, seagreen, seashell, sienna, silver, skyblue, slateblue, slategray, slategrey, snow, springgreen, steelblue, tan, teal, thistle, tomato, turquoise, violet, wheat, white, whitesmoke, yellow, yellowgreen

Built-in Plotly colorscales

Select one of the following named colorscales for coloring the Heatmap section:

‘aggrnyl’, ‘agsunset’, ‘algae’, ‘amp’, ‘armyrose’, ‘balance’, ‘blackbody’, ‘bluered’, ‘blues’, ‘blugrn’, ‘bluyl’, ‘brbg’, ‘brwnyl’, ‘bugn’, ‘bupu’, ‘burg’, ‘burgyl’, ‘cividis’, ‘curl’, ‘darkmint’, ‘deep’, ‘delta’, ‘dense’, ‘earth’, ‘edge’, ‘electric’, ‘emrld’, ‘fall’, ‘geyser’, ‘gnbu’, ‘gray’, ‘greens’, ‘greys’, ‘haline’, ‘hot’, ‘hsv’, ‘ice’, ‘icefire’, ‘inferno’, ‘jet’, ‘magenta’, ‘magma’, ‘matter’, ‘mint’, ‘mrybm’, ‘mygbm’, ‘oranges’, ‘orrd’, ‘oryel’, ‘oxy’, ‘peach’, ‘phase’, ‘picnic’, ‘pinkyl’, ‘piyg’, ‘plasma’, ‘plotly3’, ‘portland’, ‘prgn’, ‘pubu’, ‘pubugn’, ‘puor’, ‘purd’, ‘purp’, ‘purples’, ‘purpor’, ‘rainbow’, ‘rdbu’, ‘rdgy’, ‘rdpu’, ‘rdylbu’, ‘rdylgn’, ‘redor’, ‘reds’, ‘solar’, ‘spectral’, ‘speed’, ‘sunset’, ‘sunsetdark’, ‘teal’, ‘tealgrn’, ‘tealrose’, ‘tempo’, ‘temps’, ‘thermal’, ‘tropic’, ‘turbid’, ‘turbo’, ‘twilight’, ‘viridis’, ‘ylgn’, ‘ylgnbu’, ‘ylorbr’, ‘ylorrd’

Appending ‘_r’ to a named colorscale reverses it.

built-in Plotly Colorscales

You can also create your own colorscale as an array of lists, each composed of two elements. The first refers to the percentile rank, and the second to the applied color. Color can be provided in any string format among hex, hsl, rgb, or common name.

custom_colorscale = [
                      [0.00, '#636EFA'],
                      [0.25, '#AB63FA'],
                      [0.50, '#FFFFFF'],
                      [0.75, '#E763FA'],
                      [1.00, '#EF553B']

Explore px_scatter_PCA.ipynb notebook.

Plotly Graph Objects wrapper

Plotly Graph Objects is a built-in module in the plotly library and manages the Figure object that represent the entire plotting area.

import plotly.graph_objects as go

Explore go_heatmap_FQC.ipynb notebook.

Further Reading

Homepage Section Index Previous Next top of page