Show Sidebar Hide Sidebar

Bubble Charts in matplotlib

How to make a bubble chart in matplotlib. Examples of scatter charts whose markers have variable color, size, and symbols.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In [1]:
import plotly

Bubble Chart With Scatter Marker Size

Inspired by Stack Overflow

In [2]:
import plotly.plotly as py
import as tls

import matplotlib.pyplot as plt

bubbles_mpl = plt.figure()

# doubling the width of markers
x = [0,2,4,6,8,10]
y = [0]*len(x)
s = [20*4**n for n in range(len(x))]

plotly_fig = tls.mpl_to_plotly(bubbles_mpl)
py.iplot(plotly_fig, filename='mpl-bubbles')

Bubble Chart Using cmap

In [6]:
import plotly.plotly as py
import as tls

import matplotlib.pyplot as plt
import numpy as np

# decay and frequency for position and signal
w1 = 4.
w2 = 3/w1
a1 = 2
a2 = .5

# time, position, signal
t = np.linspace(0, 1, 400)
yt = np.e**(-a1*t)*np.cos(2*np.pi*w1*t)
sig = np.e**(1j*2*np.pi*w2*yt)

# open up a figure
fig, ax = plt.subplots()

# define the color map we'll use for the imaginary axis
cm ='rainbow')

# make those bubbles!
ax.scatter(t, yt, s=np.real(sig)**2*2000, c=np.imag(sig), cmap=cm)

# annotate in axes coordinates
ax.text(.97, .97, "Color = imag(sig)\nSize = real(sig)",

# label axes

plotly_fig = tls.mpl_to_plotly(fig)
py.iplot(plotly_fig, filename='mpl-bubbles-colormap')

Bubble Chart With edgecolor, linewidth, And Opacity Dimensions

In [4]:
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import as tls

n = 50
x, y, z, s, ew = np.random.rand(5, n)
c, ec = np.random.rand(2, n, 4)
area_scale, width_scale = 500, 5

fig, ax = plt.subplots()
sc = ax.scatter(x, y, s=np.square(s)*area_scale, c=c, edgecolor=ec, linewidth=ew*width_scale)

plotly_fig = tls.mpl_to_plotly(fig)
py.iplot(plotly_fig, filename='mpl-7d-bubble')


See for more information and chart attribute options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.