Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib fill_between facecolor alpha vs edgecolor alpha?

Is there a way to either specify different alphas for facecolor vs edgecolor? Or is there a way to plot an alpha filled area with non-alpha edgecolor that also works in the legend?

This is not what I want... enter image description here

axs.fill_between(xvalues, tupper_w, tlower_w, facecolor='dimgray', edgecolor='dimgray', alpha=0.25, label='$measured\quad\sigma$')
axs.fill_between(xvalues, pupper_w, plower_w, facecolor='orange', edgecolor='orange', alpha=0.25, label='$predicted\quad\sigma$')
axs.plot(xvalues, tcurvesavg_w, color='dimgray', label='$\overline{measured}$', ls='--')
axs.plot(xvalues, pcurvesavg_w, color='orange', label='$\overline{predicted}$', ls='--')


This is what I want (but with proper legend): enter image description here

axs.fill_between(xvalues, tupper, tlower, facecolor='dimgray', alpha=0.25, label='$measured\quad\sigma$')
axs.fill_between(xvalues, pupper, plower, facecolor='orange', alpha=0.25, label='$predicted\quad\sigma$')
axs.plot(xvalues, tupper, color='dimgray', lw=0.5)
axs.plot(xvalues, tcurvesavg, color='dimgray', label='$\overline{measured}$', ls='--')
axs.plot(xvalues, tlower, color='dimgray', lw=0.5)
axs.plot(xvalues, pupper, color='orange', lw=0.5)
axs.plot(xvalues, pcurvesavg, color='orange', label='$\overline{predicted}$', ls='--')
axs.plot(xvalues, plower, color='orange', lw=0.5)
like image 540
delrocco Avatar asked Jul 24 '18 00:07

delrocco


People also ask

What is the use of alpha attribute of the Fill_between () function?

The fill_between function generates a shaded region between a min and max boundary that is useful for illustrating ranges. It has a very handy where argument to combine filling with logical ranges, e.g., to just fill in a curve over some threshold value.

What does Alpha mean 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 color an area between two lines in python?

Matplotlib fill between three linesFirstly we fill the area, between y1 and y2 by using the fill_between() method and we set the color red by using the parameter color. Then we fill the area, between y2 and y3 by using the fill_between() method and we set its color to yellow by using a color parameter.

How do you fill between in Python?

fill_between() is used to fill area between two horizontal curves. Two points (x, y1) and (x, y2) define the curves. this creates one or more polygons describing the filled areas. The 'where' parameter can be used to selectively fill some areas.


2 Answers

You cannot specify different alpha values via the alpha argument. However you can define each of facecolor and edgecolor with an alpha channel, e.g. for red with 40% opacity

facecolor=(1,0,0,.4)

This is then directly applied in the legend.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2*np.pi*x)
y2 = 1.2*np.sin(4*np.pi*x)-.9

fig, ax = plt.subplots()

ax.fill_between(x, y1, y1+.5, facecolor=(1,0,0,.4), edgecolor=(0,0,0,.5), label="Label 1")
ax.fill_between(x, y2, y2-.5, facecolor=(0,0,1,.4), edgecolor=(0,0,0,.5), label="Label 1")

ax.legend()
plt.show()

enter image description here

like image 105
ImportanceOfBeingErnest Avatar answered Oct 13 '22 03:10

ImportanceOfBeingErnest


Immediately, looking at fill_between alpha and general fill_between documentation it appears to be unsupported. The legend documentation doesn't seem to provide an option for adding your border after plotting either.

In your second code snippet, if you can figure out how to get the plot and fill functions to have a single handle then the legend should automatically format. Something similar to below (adapted from this similar, but not quite duplicate StackExchangePost):

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np

xvalues = np.linspace(0,1,11)
tcurvesavg = np.linspace(0,1,11)
p1, = plt.plot(xvalues, tcurvesavg , c='r')  # notice the comma!
p2 = plt.fill_between(xvalues, tcurvesavg -0.2, tcurvesavg +0.2, color='r', alpha=0.5)
plt.legend(((p1,p2),), ('Entry',))
plt.show()

(As a non-automated workaround for most matplotlib questions, save as a svg (similar to this post) and add a border in a vector graphics program like Inkscape. You shouldn't lose resolution, and could still put it in reports etc.)

like image 40
chillindylan50 Avatar answered Oct 13 '22 04:10

chillindylan50