Intro to Data Visualization
  • Introduction
  • Getting started
    • Introduction to Pandas
    • Accessing Files on Colab
    • Reviewing Data
      • Understanding type(data) in Pandas
    • Data Types
      • Categorical Data
      • Numeric Data
      • Temporal Data
      • Geographic Data
    • How to Check Data Type
    • Slicing and Subsetting DataFrames
    • Aggregating Data
  • Visualization Types
    • Exploratory Process
    • Explanatory Process
  • data exploration
    • Exploration Overview
    • Exploration with Plotly
      • Exploring Distributions
      • Exploring Relationships
      • Exploring with Regression Plots
      • Exploring Correlations
      • Exploring Categories
      • Exploring Time Series
      • Exploring Stocks with Candlestick
      • Exploring with Facets
      • Exploring with Subplots
    • Exploring with AI
  • Data Explanation
    • Data Explanation with Plotly
      • Using Text
      • Using Annotations
      • Using Color
      • Using Shape
      • Accessibility
      • Using Animations
    • Use Cases
  • Exercises and examples
    • Stock Market
      • Loading Yahoo! Finance Data
      • Use Cases for YF
      • Exploring YF Data
      • Understanding Boeing Data Over Time
      • Polishing the visualization
      • Analyzing with AI
      • Comparisons
    • The Gapminder Dataset
      • Loading the Gapminder Data
      • Use Cases
      • Exploring the Data
      • Exporting a Static Image
Powered by GitBook
On this page
  1. data exploration

Exploration with Plotly

Exploring Data with Plotly Charts

Plotly is a versatile and interactive graphing library designed for creating high-quality, dynamic visualizations in Python and other programming languages. It supports a wide range of chart types, including scatter plots, bar charts, line graphs, 3D plots, and more advanced visualizations such as choropleth maps and dashboards. Plotly excels in creating interactive visualizations that allow users to zoom, pan, hover, and filter data points directly in the browser, making it a popular choice for building exploratory data analysis tools and data-driven applications. Its integration with Jupyter Notebooks and compatibility with various web frameworks like Dash adds to its flexibility and ease of use.

Plotly Express is a high-level interface for Plotly that simplifies the process of creating visualizations. With minimal code, users can generate insightful and interactive plots by leveraging Plotly Express's built-in functions. For example, creating a scatter plot or a bar chart often requires only one line of code, thanks to its declarative syntax. This makes Plotly Express particularly suitable for quick exploration and prototyping. Despite its simplicity, it is highly customizable, allowing users to modify color schemes, axis labels, and annotations, among other aspects. For more advanced features and customization, users can switch to Plotly’s core library. We will use Plotly Express for most of our charts.

Unlike static libraries such as Matplotlib, Plotly provides built-in interactive features without requiring additional configuration. Furthermore, its integration with Dash enables users to build web-based analytical applications with minimal effort. While libraries like Seaborn specialize in creating aesthetically pleasing static plots and ggplot2 (in R) offers a similar approach, Plotly combines beauty, interactivity, and scalability, making it a top choice for modern data visualization.

For those interested in exploring Plotly and Plotly Express further, comprehensive documentation is available:

  • Plotly Python Library Documentation

  • Plotly Express Overview

These resources provide detailed examples, tutorials, and API references. Other Python visualization libraries like Matplotlib, Seaborn, and Bokeh have their strengths—such as static publication-ready plots or enhanced styling options—but we will focus on Plotly because of its interactive capabilities.


Common Charts

Here is the code for some common charts using Plotly Express.

1. Scatter Plot

Scatter plots are useful for visualizing the relationship between two numerical variables.

import plotly.express as 
# Example: Scatter plot
fig = px.scatter(df, x='Date', y='Sales', title='Sales Scatter Plot')
fig.show()

You can also customize scatter plots by adding color or size dimensions:

# Scatter plot with color and size
fig = px.scatter(df, x='Date', y='Sales', color='Sales', size='Sales', title='Enhanced Scatter Plot')
fig.show()

2. Histogram

Histograms are used to visualize the distribution of a single variable.

# Example: Histogram
fig = px.histogram(df, x='Sales', title='Sales Distribution')
fig.show()

You can also group data in histograms by adding a color dimension:

# Grouped histogram
fig = px.histogram(df, x='Sales', color='Date', title='Grouped Sales Distribution')
fig.show()

3. Density Plot

Density plots are an alternative to histograms for visualizing distributions in a smoother form.

# Example: Density plot
fig = px.density_contour(df, x='Date', y='Sales', title='Sales Density Plot')
fig.show()

4. Box Plot

Box plots summarize the distribution of a variable and are particularly useful for detecting outliers.

# Example: Box plot
fig = px.box(df, y='Sales', title='Sales Box Plot')
fig.show()

5. Bar Chart

Bar charts are used to compare categorical data.

import pandas as pd
import plotly.express as px
# Example: Bar chart
fig = px.bar(df, x='Date', y='Sales', title='Daily Sales Bar Chart')
fig.show()

6. Heatmap

Heatmaps are useful for visualizing correlations or density across two variables.

# Example: Heatmap
import numpy as np
import pandas as pd
import plotly.express as px

heatmap_data = pd.DataFrame(
    np.random.rand(5, 5),
    columns=['A', 'B', 'C', 'D', 'E']
)
fig = px.imshow(heatmap_data, title='Heatmap Example')
fig.show()

PreviousExploration OverviewNextExploring Distributions

Last updated 3 months ago