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.

# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

import matplotlib.pyplot as plt
import plotly.plotly as py

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)

plot_url = py.plot_mpl(bubbles_mpl, filename='mpl-bubbles')
Inspired by Stack Overflow.
import matplotlib.pyplot as plt
import numpy as np
import plotly.plotly as py

# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

# 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')

plot_url = py.plot_mpl(fig, filename='mpl-bubble-colormap')
import matplotlib.pyplot as plt
import numpy as np
import plotly.plotly as py
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

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()

plot_url = py.plot_mpl(fig, filename='mpl-7d-bubble')
Still need help?
Contact Us

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