Search code examples
pythonmatplotlibplotalpha

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?


Solution

  • 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