<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Matplotlib
Matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.
We can produce specific types of figures such as bar charts, pie charts, histograms, scatter plots, etc.
These can be output in a range of formats (SVG / PDF / PNG).
In this section we'll cover the basics of producing plots. We'll show and customize a set of default properties that comes with matplotlib, colour, axis, labels, style, etc, and present a more elaborate cases that show how well matplotlib mingles with python.
First One of iPython Notebooks 'magic commands'.
%matplotlib inline puts matplotlib into the cell output rather than creating a popup window.
Step1:  Libraries we will be using
Step2:  A basic matplotlib using Python's range function for data
Step3:  A scatter plot with using NumPy's random function to create data
Step4:  A bar chart
Step5:  A scatter plot with colours, area and alpha blending
Step6:  Changing matplotlib default output
Basic plot
Step7:  Setting the defaults explictily
Step8:  Increasing size, x y limits and change colours
Step9:  Set tick values and Latex
Step10:  Label Plot and Axis
Step11:  Moving spines
Step12:  Adding a Legend
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
Explanation: Matplotlib
Matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.
We can produce specific types of figures such as bar charts, pie charts, histograms, scatter plots, etc.
These can be output in a range of formats (SVG / PDF / PNG).
In this section we'll cover the basics of producing plots. We'll show and customize a set of default properties that comes with matplotlib, colour, axis, labels, style, etc, and present a more elaborate cases that show how well matplotlib mingles with python.
First One of iPython Notebooks 'magic commands'.
%matplotlib inline puts matplotlib into the cell output rather than creating a popup window.
End of explanation
import numpy as np
import matplotlib.pyplot as plt
Explanation: Libraries we will be using:
End of explanation
plt.plot(range(20))
Explanation: A basic matplotlib using Python's range function for data
End of explanation
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
plt.scatter(x, y)
plt.show()
Explanation: A scatter plot with using NumPy's random function to create data
End of explanation
y = np.random.rand(5)
x = np.arange(5)
plt.bar(x,y)
plt.show()
Explanation: A bar chart
End of explanation
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radiuses
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.colorbar()
plt.show()
Explanation: A scatter plot with colours, area and alpha blending
End of explanation
X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)
plt.plot(X,C)
plt.plot(X,S)
plt.show()
Explanation: Changing matplotlib default output
Basic plot
End of explanation
# Create a new subplot from a grid of 1x1
plt.subplot(111)
# Plot cosine using blue color with a continuous line of width 1 (pixels)
plt.plot(X, C, color="blue", linewidth=1.0, linestyle="-")
# Plot sine using green color with a continuous line of width 1 (pixels)
plt.plot(X, S, color="green", linewidth=1.0, linestyle="-")
# Set x limits
plt.xlim(-4.0,4.0)
# Set x ticks
plt.xticks(np.linspace(-4,4,9,endpoint=True))
# Set y limits
plt.ylim(-1.0,1.0)
# Set y ticks
plt.yticks(np.linspace(-1,1,5,endpoint=True))
# Save figure using 72 dots per inch
# savefig("../exercice_2.png",dpi=72)
# Show result on screen
plt.show()
Explanation: Setting the defaults explictily
End of explanation
plt.figure(figsize=(10,6), dpi=80)
plt.xlim(X.min()*1.1, X.max()*1.1)
plt.ylim(C.min()*1.1, C.max()*1.1)
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red",  linewidth=2.5, linestyle="-")
plt.show()
Explanation: Increasing size, x y limits and change colours
End of explanation
plt.figure(figsize=(10,6), dpi=80)
plt.xlim(X.min()*1.1, X.max()*1.1)
plt.ylim(C.min()*1.1, C.max()*1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], 
           [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
plt.yticks([-1, 0, +1],
           [r'$-1$', r'$0$', r'$+1$'])
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red",  linewidth=2.5, linestyle="-")
plt.show()
Explanation: Set tick values and Latex
End of explanation
plt.figure(figsize=(10,6), dpi=80)
plt.xlim(X.min()*1.1, X.max()*1.1)
plt.ylim(C.min()*1.1, C.max()*1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], 
           [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
plt.yticks([-1, 0, +1],
           [r'$-1$', r'$0$', r'$+1$'])
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red",  linewidth=2.5, linestyle="-")
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('sin/cos')
plt.show()
Explanation: Label Plot and Axis
End of explanation
ax = plt.gca()
plt.xlim(X.min()*1.1, X.max()*1.1)
plt.ylim(C.min()*1.1, C.max()*1.1)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red",  linewidth=2.5, linestyle="-")
plt.show()
Explanation: Moving spines
End of explanation
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plt.plot(X, S, color="red",  linewidth=2.5, linestyle="-", label="sine")
plt.xlim(X.min()*1.1, X.max()*1.1)
plt.ylim(C.min()*1.1, C.max()*1.1)
plt.legend(loc='upper left', frameon=False)
plt.show()
Explanation: Adding a Legend
End of explanation
<END_TASK>