# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] slideshow={"slide_type": "slide"}
# # Matplotlib
# ## Introduction
# - matplotlib is probably the single most used Python package for 2D-graphics
# - it also provides good capablities to creade 3D-graphics
# - quick way to visualize data from python in publication-quality
#
# - for further information: https://matplotlib.org/

# + [markdown] slideshow={"slide_type": "slide"}
# ## Creating First Plots
#
# ### 1. Import pyplot package
#     - provides functions that makes matplotlib work like MATLAB
#     - object-oriented plotting

# + slideshow={"slide_type": "fragment"}
import matplotlib.pyplot as plt # import pyplot interface

# + [markdown] slideshow={"slide_type": "subslide"}
# ### 3. Create [figure](https://matplotlib.org/api/figure_api.html) and [axes](https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html)

# + slideshow={"slide_type": "subslide"}
fig = plt.figure() # a new figure window
ax = fig.add_subplot(1, 1, 1) # a new axes
plt.show(fig)

# + [markdown] slideshow={"slide_type": "subslide"}
# ### 3. Create / [Plot data](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html) (sine)

# + slideshow={"slide_type": "-"}
import numpy as np
x = np.linspace(0,10,1000)
y = np.sin(x)

ax.plot(x,y, label = 'sine')
fig # this is required to re-display the figure

# + [markdown] slideshow={"slide_type": "subslide"}
# #### Customize Line Style

# + slideshow={"slide_type": "-"}
fig2 = plt.figure() # a new figure window
ax2 = fig2.add_subplot(1, 1, 1) # a new axes

x2 = np.linspace(0,10,50)
y2 = np.sin(x2)

ax2.plot(x2,y2, '-o', label = 'sine')
plt.show(fig2)

# + slideshow={"slide_type": "subslide"}
fig3 = plt.figure() # a new figure window
ax3 = fig3.add_subplot(1, 1, 1) # a new axes

x2 = np.linspace(0,10,50)
y2 = np.sin(x2)

ax3.plot(x2,y2, 'r-o', label = 'sine')
plt.show(fig3)

# + [markdown] slideshow={"slide_type": "subslide"}
# ##### Line Colour
# 'r': red  
# 'g': green  
# 'b': blue  
# 'c': cyan  
# 'm': magenta  
# 'y': yellow  
# 'k': black  
# 'w:': white  

# + [markdown] slideshow={"slide_type": "subslide"}
# ##### Line Style
# '-': solid  
# '--': dashed  
# ':': dotted  
# '-.': dot-dashed  
# '.': points  
# 'o': filled circles  
# '^': filled triangles  

# + [markdown] slideshow={"slide_type": "subslide"}
# ### 4. Create / [Plot data](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html) (cosine)

# + slideshow={"slide_type": "-"}
y2 = np.cos(x)
ax.plot(x,y2, label = 'cosine')

fig

# + [markdown] slideshow={"slide_type": "subslide"}
# ### 5. Create / [Plot data](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html) (3 * cosine) on second axes

# +
ax_twin = ax.twinx()
y3 = 3 * np.cos(x+np.pi/4)
ax_twin.plot(x,y3, 'r',label = '3 * cosine')

fig

# + [markdown] slideshow={"slide_type": "subslide"}
# ### 5. Set limits for [x](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_xlim.html)-/[y](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_ylim.html)-axis

# + slideshow={"slide_type": "-"}
ax.set_xlim(0,10)
ax.set_ylim(-1.5, 2.0)
fig

# + [markdown] slideshow={"slide_type": "subslide"}
# ### 6. [Add legend](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html)

# + slideshow={"slide_type": "-"}
ax.legend()
fig

# + [markdown] slideshow={"slide_type": "subslide"}
# ### 7. Add [x](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_xlabel.html)-/[y](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_ylabel.html)-label and [title](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_title.html)

# + slideshow={"slide_type": "-"}
ax.set_xlabel("$x$")
ax.set_ylabel("$\sin(x)$")
ax.set_title("I like $\pi$")

fig

# + [markdown] slideshow={"slide_type": "subslide"}
# ### 7. [Add grid](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.grid.html)
# -

ax.grid(True)
fig

# + [markdown] slideshow={"slide_type": "subslide"}
# ### Excursion Subplots
# - the command [fig.add_subplot](https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html) divides the figures in grid with a certain number of axes
# - syntax:
# ``` python    
# fig.add_subplot(rows, cols, num)
# ```    
# - rows = number of rows in the grid
# - cols = number of columns in the grid
# - num = number of the subplot to create (counting from left to right, top to bottom and indexed starting at 1)

# + slideshow={"slide_type": "subslide"}
fig  =  plt.figure()
for  i  in range(6):
    ax  =  fig.add_subplot(2, 3, i + 1)
    ax.set_title("Plot #%i" % i)

# + [markdown] slideshow={"slide_type": "subslide"}
# - the subplots are overlapping
# - there are a few ways to fix it, i.e.:

# + slideshow={"slide_type": "-"}
fig.subplots_adjust(wspace=0.4, hspace=0.4)
fig

# + [markdown] slideshow={"slide_type": "-"}
# - ```wspace``` and ```hspace ``` determine the width and height between each plot

# + [markdown] slideshow={"slide_type": "slide"}
# ## 2. Various 2D Plotting
# ### [Histograms](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html)

# + slideshow={"slide_type": "-"}
x = np.random.normal(size=1000)

fig, ax = plt.subplots()

H = ax.hist(x, bins=50, alpha=0.5, histtype='stepfilled')

# + [markdown] slideshow={"slide_type": "subslide"}
# ### [Pie Plot](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pie.html)

# + slideshow={"slide_type": "-"}
fracs = [30, 15, 45, 10]
colors = ['b', 'g', 'r', 'w']

fig, ax = plt.subplots(figsize=(6, 6))  # make the plot square
pie = ax.pie(fracs, colors=colors, explode=(0, 0, 0.05, 0), shadow=True,
             labels=['A', 'B', 'C', 'D'])

# + [markdown] slideshow={"slide_type": "subslide"}
# ### [Errorbar Plots](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.errorbar.html)

# + slideshow={"slide_type": "-"}
x = np.linspace(0, 10, 30)
dy = 0.1
y = np.random.normal(np.sin(x),dy)

fig, ax = plt.subplots()
plt.errorbar(x, y, dy, fmt='.k')

# + [markdown] slideshow={"slide_type": "subslide"}
# ### [Contour Plots (filled)](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.contourf.html)

# + slideshow={"slide_type": "-"}
x = np.linspace(0, 10, 50)
y = np.linspace(0, 20, 60)

z = np.cos(y[:, np.newaxis]) * np.sin(x)

fig, ax = plt.subplots()

# filled contours
im = ax.contourf(x, y, z, 100)

fig.colorbar(im, ax=ax)

# + [markdown] slideshow={"slide_type": "subslide"}
# ### [Contour Plots (lines)](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.contour.html)

# + slideshow={"slide_type": "-"}
# contour lines
im2 = ax.contour(x, y, z, colors='k')

fig

# + [markdown] slideshow={"slide_type": "slide"}
# ## 3. [Various 3D Plotting](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html)

# + slideshow={"slide_type": "-"}
# This is the 3D plotting toolkit
from mpl_toolkits.mplot3d import Axes3D

# + [markdown] slideshow={"slide_type": "subslide"}
# ### [3D scatter Plot](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#scatter-plots)

# + slideshow={"slide_type": "-"}
fig = plt.figure()
ax = plt.axes(projection='3d')

z = np.linspace(0, 1, 100)
x = z * np.sin(20 * z)
y = z * np.cos(20 * z)

c = x + y

ax.scatter(x, y, z, c=c)

# + [markdown] slideshow={"slide_type": "subslide"}
# ### [3D Line Plot](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#line-plots)

# + slideshow={"slide_type": "-"}
fig = plt.figure()
ax = plt.axes(projection='3d')

ax.plot(x, y, z, '-b')

# + [markdown] slideshow={"slide_type": "subslide"}
# ### [Surface Plot](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#surface-plots)

# +
x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
y = x.copy().T
z = np.cos(x ** 2 + y ** 2)

fig = plt.figure()
ax = plt.axes(projection='3d')

ax.plot_surface(x, y, z, cmap=plt.cm.jet, rstride=1, cstride=1, linewidth=0)

StarcoderdataJupyterScriptsDedupFiltered