Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partially transparent scatter plot, but with a solid color bar

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…

like image 922
Eric O Lebigot Avatar asked Dec 18 '10 15:12

Eric O Lebigot


People also ask

How do you make a scatter plot transparent?

Utilize the alpha argument in our scatter method and pass in a numeric value between 0 and 1. A value of 0 will make the plots fully transparent and unable to view on a white background.

How do I make a color transparent in Matplotlib?

Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.

How do I change the color of my scatter PLT?

To change the color of a scatter point in matplotlib, there is the option "c" in the function scatter.


2 Answers

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! :)

like image 163
Eric O Lebigot Avatar answered Sep 23 '22 15:09

Eric O Lebigot


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

like image 33
Steve Tjoa Avatar answered Sep 22 '22 15:09

Steve Tjoa