Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the alpha value for matplotlib plots globally

Using Jupyther Notebooks i often find myself repeadily writing the following to change the alpha value of the plots:

plot(x,y1, alpha=.6)
plot(x,y2, alpha=.6)
...

I was hoping to find a matching value in the rcParameters to change to option globally like:

plt.rcParams['lines.alpha'] = 0.6 #not working

What is a possible workaround to change the alpha value for all plots?

like image 535
Jul3k Avatar asked Sep 24 '15 09:09

Jul3k


2 Answers

Unfortunately, based on their How to entry:

If you need all the figure elements to be transparent, there is currently no global alpha setting, but you can set the alpha channel on individual elements.

So, via matplotlib there's currently no way to do this.


What I usually do for global values is define an external configuration file, define values and import them to the appropriate scripts.

my_conf.py

# Parameters:

# matplotlib alpha
ALPHA = .6

my_plots.py

import conf.py as CONF

plot(x,y1, alpha=CONF.ALPHA)
plot(x,y2, alpha=CONF.ALPHA)

This usually helps in keeping configuration separated and easy to update.

like image 94
Dimitris Fasarakis Hilliard Avatar answered Sep 19 '22 05:09

Dimitris Fasarakis Hilliard


Answering my own question with help of the matplotlib team the following code will do the job by changeing the alpha value of the line colors globally:

alpha = 0.6
to_rgba = matplotlib.colors.ColorConverter().to_rgba

for i, col in enumerate(plt.rcParams['axes.color_cycle']):
    plt.rcParams['axes.color_cycle'][i] = to_rgba(col, alpha)

Note: In matplotlib 1.5 color_cycle will be deprecated and replaced by prop_cycle

The ability the set the alpha value over the rcParams has also been added to the wishlist for Version 2.1

like image 23
Jul3k Avatar answered Sep 19 '22 05:09

Jul3k