Partially transparent scatter plot, but with a solid color bar
Asked Answered
E

4

49

In Python, with Matplotlib, how to simply do a scatter plot with transparency (alpha < 1), but with a color bar that represents their color value, but has alpha = 1?

Here is what one gets, with from pylab import *; scatter(range(10), arange(0, 100, 10), c=range(10), alpha=0.2); color_bar = colorbar():

alt text

How can the color bar be made non-transparent?

PS: I tried color_bar.set_alpha(1); draw(), but this did not do anything…

Equitation answered 18/12, 2010 at 15:44 Comment(2)
But since it's a scatter plot, what would the color bar indicate? Should it correspond to the size of each point, s, or the color of each point, c?Autograph
@Steve: The color bar would map the color of the points.Equitation
S
4

Joe Kington's comment is the correct way to do this as matplotlib 6.0 has deprecated cbar.draw_all():

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
im = ax.scatter(range(10), range(10), c=range(10), alpha=0.2)
cbar = fig.colorbar(im, ax=ax)
cbar.solids.set(alpha=1)

The deprecation warning:
MatplotlibDeprecationWarning: The draw_all function was deprecated in Matplotlib 3.6 and will be removed two minor releases later. Use fig.draw_without_rendering() instead. cbar.draw_all()

Sabbatarian answered 6/8, 2023 at 22:57 Comment(3)
Interesting. Could you share a reference to the deprecation? I'll be ready to change the accepted answer. :)Equitation
@EricO.Lebigot I've added the depreciation warning to my post.Sabbatarian
@joe-kington happy for you to make your own answer... it is your work after all :)Sabbatarian
E
48

Alright, I found one way to do it, that looks relatively clean: (using the ColorBar object from the question)

color_bar.set_alpha(1)
color_bar.draw_all()
# pylab.draw() or pyplot.draw() might be necessary

It would be great to get a confirmation that this is the most robust way to proceed, though! :)

Equitation answered 18/12, 2010 at 20:55 Comment(4)
What if I don't want the colorbar labels? draw_all() makes the labels appear even if I have set color_bar.ax.set_yticklabels([]).Robena
I'm not sure how to do this… I tried to clear the color bar axes first (color_bar.ax.cla()), but then drawing the colorbar again with draw_all() fails.Equitation
On a side note, you can also use cbar.solids.set(alpha=1) if you'd prefer not to call draw_all() for any particular reason (e.g. custom labels, etc).Epistasis
Note that @JoeKington's solution does not apply the alpha to the outer limits of the colorbar (if extend='both" was used)Lindbom
A
15

This is a huge, ugly hack. But no other way would work. Maybe someone else can improve.

fig1 = pylab.figure()
fig2 = pylab.figure()
ax1 = fig1.add_subplot(111)
ax2 = fig2.add_subplot(111)
ax1.scatter(range(10), range(10), c=range(10), alpha=0.2)
im = ax2.scatter(range(10), range(10), c=range(10), alpha=1.0)
fig1.colorbar(im, ax=ax1)
fig1.show()

alt text

Autograph answered 18/12, 2010 at 20:27 Comment(2)
+1: interesting idea! One drawback is that you get an additional figure when pyplot.show() is called (that could be destroyed, arguably…).Equitation
Indeed, it creates a new figure. That was really a "last resort" solution. :-)Autograph
S
4

Joe Kington's comment is the correct way to do this as matplotlib 6.0 has deprecated cbar.draw_all():

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
im = ax.scatter(range(10), range(10), c=range(10), alpha=0.2)
cbar = fig.colorbar(im, ax=ax)
cbar.solids.set(alpha=1)

The deprecation warning:
MatplotlibDeprecationWarning: The draw_all function was deprecated in Matplotlib 3.6 and will be removed two minor releases later. Use fig.draw_without_rendering() instead. cbar.draw_all()

Sabbatarian answered 6/8, 2023 at 22:57 Comment(3)
Interesting. Could you share a reference to the deprecation? I'll be ready to change the accepted answer. :)Equitation
@EricO.Lebigot I've added the depreciation warning to my post.Sabbatarian
@joe-kington happy for you to make your own answer... it is your work after all :)Sabbatarian
S
0

This worked for me:

import matplotlib.pyplot as plt
import numpy as np

plt.figure()
p = plt.scatter(np.arange(10), np.arange(0,100,10), c=np.arange(10), alpha=1)
plt.colorbar()
p.set_alpha(0.2)
plt.show()
Scourge answered 4/4 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.