Customizable Scatter Plot Visualization

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this tutorial, we will learn how to create a scatter plot with varying marker colors and sizes using Python Matplotlib.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} matplotlib/importing_matplotlib -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} matplotlib/figures_axes -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} matplotlib/scatter_plots -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} matplotlib/grid_config -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} python/booleans -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} python/lists -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} python/tuples -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} python/sets -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} python/importing_modules -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} python/file_opening_closing -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} python/numerical_computing -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} python/data_visualization -.-> lab-48912{{"`Customizable Scatter Plot Visualization`"}} end

Import necessary libraries

We will start by importing the necessary libraries, which are Matplotlib and Numpy.

import matplotlib.pyplot as plt
import numpy as np

Load data

We will load a numpy record array from yahoo csv data with fields date, open, high, low, close, volume, adj_close from the mpl-data/sample_data directory. The record array stores the date as an np.datetime64 with a day unit ('D') in the date column.

import matplotlib.cbook as cbook

price_data = cbook.get_sample_data('goog.npz')['price_data'].view(np.recarray)
price_data = price_data[-250:]  ## get the most recent 250 trading days

Calculate values for scatter plot

We will calculate delta1, volume, and close values for scatter plot.

delta1 = np.diff(price_data.adj_close) / price_data.adj_close[:-1]

## Marker size in units of points^2
volume = (15 * price_data.volume[:-2] / price_data.volume[0])**2
close = 0.003 * price_data.close[:-2] / 0.003 * price_data.open[:-2]

Create scatter plot

We will create a scatter plot with varying marker colors and sizes using the calculated values.

fig, ax = plt.subplots()
ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5)

ax.set_xlabel(r'$\Delta_i$', fontsize=15)
ax.set_ylabel(r'$\Delta_{i+1}$', fontsize=15)
ax.set_title('Volume and percent change')

ax.grid(True)
fig.tight_layout()

plt.show()

Summary

We have learned how to create a scatter plot with varying marker colors and sizes using Python Matplotlib. We started by importing the necessary libraries, then loading data, calculating values for scatter plot, and creating the scatter plot.

Other Python Tutorials you may like