Reputation: 1096
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?
Upvotes: 8
Views: 12107
Reputation: 54
Another way to do it is just to specify the alpha and pass this to the rcParams
plt.rcParams['axes.prop_cycle'] = cycler(alpha=[0.5])
Bear in mind that this can be combined with any other cycling properties such as the color and line style.
cyc_color = cycler(color=['r','b','g')
cyc_lines = cycler(linestyle=['-', '--', ':'])
cyc_alpha = cycler(alpha=[0.5, 0.3])
cyc = (cyc_alpha * cyc_lines * cyc_color)
Be careful with the order of your cyclers, as the sequence above will cycle through colors, then lines, then alphas.
Upvotes: 0
Reputation: 1789
Updated version (perhaps cleaner available) :
from cycler import cycler
alpha = 0.5
to_rgba = matplotlib.colors.ColorConverter().to_rgba#
color_list=[]
for i, col in enumerate(plt.rcParams['axes.prop_cycle']):
color_list.append(to_rgba(col['color'], alpha))
plt.rcParams['axes.prop_cycle'] = cycler(color=color_list)
Upvotes: 1
Reputation: 1096
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
Upvotes: 2
Reputation: 160657
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.
Upvotes: 7