# 76. Matplotlib: Adding shaded areas to charts

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()
```

# 50. Matplotlib: Adding contour lines to a heatmap

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”

# 47. Linear regression with scipy.stats

``````%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```

# 44. Matplotlib: Common modifications to charts

Here we show some common modifications to charts. These include:

• Changing scatter plot point style
• Changing line plot line and marker style
• Adding a legend
• Adding some text
• Changing axis scales
• Changing axis ticks
• Adding a grid
• Adding axis tiles
• Adding chart title

# 43. Matplotlib: 3D wireframe and surface plots

We can create 3D wireframe or surface plots easily in MatplotLib

# 42. Matplotlib: Boxplots

Matplotlib allows easy creation of boxplots. These traditionally show median (middle line across box), uper and lower quartiles (box), range excluding outliers (whiskers) and outliers (points). The default setting for outliers is points more than 1.5xIQR above or below the quartiles.

*IQR = inter-quartile range. Continue reading “42. Matplotlib: Boxplots”