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()
Violin plots are an alternative to box plots. They show the spread of data in the form of a distribution plot along the y axis. Some people love them. Others don’t! See what you think. Continue reading “52. Matplotlib: Violin plots”
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”
%matplotlib inline import numpy as np import matplotlib.pyplot as plt from scipy import stats # Set up x any arrays x=np.array([1,2,3,4,5,6,7,8,9,10]) y=np.array([2.3,4.5,5.0,8,11.1,10.9,13.9,15.4,18.2,19.5]) y=y+10 # scipy linear regression gradient, intercept, r_value, p_value, std_err = stats.linregress(x,y) # Calculated fitted y y_fit=intercept + (x*gradient) # Plot data plt.plot(x, y, 'o', label='original data') plt.plot(x, y_fit, 'r', label='fitted line') # Add text box and legend text='Intercept: %.1f\nslope: %.2f\nR-square: %.3f' %(intercept,gradient,r_value**2) plt.text(6,15,text) plt.legend() # Display plot plt.show()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”