{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MCVQrt2M6Kbg"
},
"source": [
"# Chapter 7 - Go Beyond Plotly Express\n",
"\n",
"Plotly Express is convenient and fast, but it can only take you to select designations. To go where Plotly Express cannot reach, you can resort to Plotly, the foundational library that Plotly Express was built on.\n",
"\n",
"Since Plotly Express does not provide a Pareto chart, let's build one from scratch using Plotly.\n",
"\n",
"Using 2020 population of countries as an example.\n",
"\n",
"Demonstrate the use of subplots with secondary Y axis.\n",
"\n",
"Demonstrate the use of color scales and how to hide the scale."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cN8ayVueywnO",
"outputId": "ac1789ce-16d6-4ca1-c2cc-ad21e847e66d"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: plotly in /usr/local/lib/python3.7/dist-packages (5.3.1)\n",
"Requirement already satisfied: tenacity>=6.2.0 in /usr/local/lib/python3.7/dist-packages (from plotly) (8.0.1)\n",
"Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from plotly) (1.15.0)\n"
]
}
],
"source": [
"# As of this writing, the Google Colab has Plotly version 4.4.1 pre-installed\n",
"# We need to upgrade it to the latest version\n",
"\n",
"!pip install --upgrade plotly"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qKNd5wkciFWE"
},
"source": [
"To note, for this chapter, we will not import Plotly Express module. WE will import Plotly's graph_objects module instead. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "9xMXPdyEZQvS",
"outputId": "51d33d30-4f40-4c88-fd3e-c91b68560178"
},
"outputs": [
{
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'5.3.1'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np # We use numpy to generate some sample data for ploting\n",
"import plotly.graph_objects as go # graph_opjects package is the core of plotly\n",
"import plotly.io as pio\n",
"\n",
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WR1HbwfL50ha"
},
"source": [
"## 7.1 Plotly Basics\n",
"\n",
"Plotly uses Java Script Object Notation (JSON) format to describe how data are visualized. JSON is a standard format for web applications and data integrations. It is similar to Python's dictionary object and uses key-value pairs to describe data and computing instructions.\n",
"\n",
"A Plotly data visualization is represented by a **Figure** object. A figure has two components: **Data** and **Layout**. \n",
"\n",
"The data component is is a list of **Traces**. A trace describes any predefined type of charts such as boxplot, bar chart, and scatter plot and any custom-coded type of charts.\n",
"\n",
"The layout component describes the overall characteristics of a figure such as its title, legend, and titles of the axes among many others. \n",
"\n",
"A sophisticated visualization can be implemented by incorporating multiple traces each representing a unique visual component with customized layout.\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "X1e_erkW6Y69"
},
"source": [
"## 7.2 A \"Hello World\" Chart\n",
"This example uses the method `update_layout()` of Figure class to add a title for the figure as well as the X axis and Y axis.\n",
"\n",
"This simple chart has no data to display. \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 542
},
"id": "r5cSLIn95yA0",
"outputId": "3eab0538-d90a-4bb3-fe84-ecc01445ff82"
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"