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
plotly.__version__
Out[1]:
'3.1.1'

Bubble Chart With Scatter Marker Size

Inspired by Stack Overflow

In [2]:
import plotly.plotly as py
import plotly.tools 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))]
plt.scatter(x,y,s=s)

plotly_fig = tls.mpl_to_plotly(bubbles_mpl)
py.iplot(plotly_fig, filename='mpl-bubbles')
Out[2]:

Bubble Chart Using cmap

In [6]:
import plotly.plotly as py
import plotly.tools 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 = plt.cm.get_cmap('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)",
        transform=ax.transAxes,
        horizontalalignment='right',
        verticalalignment='top',
        size=11)

# label axes
ax.set_ylabel('position')
ax.set_xlabel('time')

plotly_fig = tls.mpl_to_plotly(fig)
py.iplot(plotly_fig, filename='mpl-bubbles-colormap')
Out[6]:

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 plotly.tools 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)
ax.grid()

plotly_fig = tls.mpl_to_plotly(fig)
py.iplot(plotly_fig, filename='mpl-7d-bubble')
Out[4]:

Reference

See https://plot.ly/python/reference/ for more information and chart attribute options!

Still need help?
Contact Us

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