Skip to content
Home » matplotlib Part 25 – The Colorbar for Scatter Plots

matplotlib Part 25 – The Colorbar for Scatter Plots

Spread the love

The Colorbar for Scatter Plots

Some time ago we were talking about scatter plots. In the previous article we were talking about colorbars. In this article we’ll use a colorbar for the colors of the points.

Let’s start by creating a scatter plot. The points will be circles differing in color and size:

In [2]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(10, 50, 100)

# Let's randomize the position of Y values.
a = np.random.randn(100)
y = x * a

# Let's set alpha to 0.5. The size and color will depend on the value of Y.
fig, ax = plt.subplots()
ax.scatter(x, y, marker='o', alpha=.5, s=y*50, c=y)
Out[2]:
<matplotlib.collections.PathCollection at 0x229dcb92848>

Now we have a nice scatter plot. All we need to do is add the legend. Let’s create a colorbar then:

In [3]:
fig, ax = plt.subplots()

# Let's assign the scatter plot to a variable.
p = ax.scatter(x, y, marker='o', alpha=.5, s=y*50, c=y)

# Now we can use the variable to create the colorbar.
fig.colorbar(p)
Out[3]:
<matplotlib.colorbar.Colorbar at 0x229df0b1448>

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply