plt.fill_between may be used to add shaded areas to charts. By using the alpha (transparency) argument shaded areas may overlap.
import matplotlib.pyplot as plt y1 = [60, 65, 65, 70, 75] y1_max = [70, 73, 70, 78, 82] y1_min = [55, 58, 61, 62, 68] y2 = [45, 50, 55, 55, 55] y2_max = [53, 55, 63, 60, 62] y2_min = [35, 45, 52, 52, 50] plt.plot(x, y1) plt.plot(x, y2, linestyle ='dashed') # alpha adjusts transparency, higher alpha --> darker grey # Or color could be set to, for example '0.2', but using transparency allows # overlapping shaded areas plt.fill_between(x, y1_min, y1_max, color = 'k', alpha = 0.1) plt.fill_between(x, y2_min, y2_max, color = 'k', alpha = 0.1) plt.show()
Adding error bars to line plots
Here will will add contour lines to a heat map.
We’ll use something a little more interesting for the array of values, we’ll define a Mandlebrot fractal function. We have build a 1,000 and 1,000 array and calculate z as a Mandlebrot function of x and y.
The heatmap is drawn with plt.imshow, and then contour lines are added with plt.contour. Continue reading “50. Matplotlib: Adding contour lines to a heatmap”
This post is also available as a PDF and as a Jupyter Notebook. Continue reading “47. Linear regression with scipy.stats”
There are various ways of creating subplots in Matplotlib.
Here we will use add_subplot to bring four plots together.
It is also worth looking at subplot2grid if you want plots of different sizes bought together. Continue reading “46. Matplotlib: Creating a grid of subplots”