GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • PYTHON CHARTS
  • matplotlib
  • seaborn
  • plotly
  • pandas
  • colors
  • COLORS
  • COLOR PALETTES
  • R CODER
  • R CHARTS
  • Contact
  • Español

This view

  • Original page
  • New address

GeezerVison is showing python-charts.com — Bubble chart in matplotlib

Search for a graph

HOME CORRELATION BUBBLE CHART MATPLOTLIB

Bubble chart in matplotlib

Sample data

Consider the following data for illustration purposes:

import numpy as np

# Seed
rng = np.random.RandomState(1)

# Data simulation
x = rng.uniform(0, 1, 40)
y = rng.uniform(0, 1, 40)
size = rng.uniform(20, 60, 40)

Bubble chart in matplotlib with scatter

A bubble chart is a scatter plot where a third numeric variable is mapped to the size of the markers. Matplotlib does not need a dedicated function: just pass an array to the s argument of scatter.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(x, y, s = size)

# plt.show()

Bubble chart in matplotlib

Size scale

The values passed to s are the marker area in points, not a relative scale, so you will often need to multiply your variable by a constant to get a readable range of bubble sizes.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(x, y, s = size * 10)

# plt.show()

Size scale of a bubble chart in matplotlib

Colors

Map a fourth variable to c together with a cmap to also encode color, or pass a single fixed color for every bubble.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(x, y, s = size * 10, c = size, cmap = "viridis")

# plt.show()

Colors of a bubble chart in matplotlib

Transparency and edge color

Overlapping bubbles are easier to read with some transparency (alpha) and a border (edgecolors).

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(x, y, s = size * 10, color = "#4C72B0",
          alpha = 0.6, edgecolors = "white")

# plt.show()

Transparency and edge color of a bubble chart in matplotlib

Size legend

Since scatter does not create a size legend automatically, build one manually with legend_elements, which derives sample handles from the plotted sizes.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
scatter = ax.scatter(x, y, s = size * 10, color = "#4C72B0", alpha = 0.6)

handles, labels = scatter.legend_elements(prop = "sizes", num = 4, alpha = 0.6)
ax.legend(handles, labels, title = "Size", loc = "upper right")

# plt.show()

Size legend of a bubble chart in matplotlib

R CODER website
Data Sketches

Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon
Data visualization

Data visualization

A practical introduction

Buy on Amazon

See also

Scatter plot in matplotlib

Scatter plot in matplotlib

Histograms in plotly

Histograms in plotly

Scatter plot in plotly

Scatter plot in plotly

Bubble chart in plotly

Bubble chart in plotly

Scatter plot in seaborn

Scatter plot in seaborn

Heatmaps in plotly with imshow

Heatmaps in plotly with imshow

Connected scatter plot in matplotlib

Connected scatter plot in matplotlib

Hexbin chart in matplotlib

Hexbin chart in matplotlib

Heatmap clustering in seaborn with clustermap

Heatmap clustering in seaborn with clustermap

Contour in matplotlib

Contour in matplotlib

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.