# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.3'
#       jupytext_version: 1.14.0
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# %% [markdown]
# # Matplotlib (Teil 2)

# %% [markdown]
# Hinzufügen von Achsenbezeichnungen und einer Bildüberschrift:

# %%
import numpy as np
import matplotlib.pyplot as plt

# %%
import matplotlib
matplotlib.rcParams['figure.figsize'] = (4., 3.)
plt.rcParams['figure.figsize'] = (4., 3.)


# %%
def f(t):
    return t**2*np.exp(-t**2)

t = np.linspace(0,3,51)
y = f(t)

fig = plt.figure(1)
ax = fig.gca()
ax.plot(t,y)
ax.set_xlabel('t')
ax.set_ylabel('f(t)')
ax.set_title('Titel');

# %% [markdown]
# Bild als File speichern:
#
# __savefig__

# %%
fig.savefig('bild1.pdf') # das format  wird anhand der Endung geraten

# %%
fig.savefig('bild1.svg', format='svg')

# %%
fig.canvas.get_supported_filetypes_grouped()

# %% [markdown]
# Subplots erzeugen:
#
# __subplots__ erzeugt mehrere Koordinatensysteme hier 1 x 3

# %%
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, clear = True, figsize=(6, 2))

x = np.linspace(-4, 4, 101)
ax1.plot(np.sin(x), np.cos(x))

y2 = np.exp(-x**2)
ax2.plot(x, y2)

y3 = np.tanh(x)
y4 = np.cos(x**2)
ax3.plot(x, y3, label='tanh')
ax3.plot(x, y4, 'r', label='$\cos(x^2)$')

ax1.set_title('links')
ax1.set_ylabel('y')
ax2.set_xlabel('x Achse')
ax2.axis([-5, 4, 0, 1]) # mit axis([xmin, xmax, ymin, ymax]) wird der Ausschnitt im Koordinatensystem geändert 
ax1.axis('equal');      # x- und y-Achse werden gleich skaliert
ax3.legend(loc=1)       # loc legt den Ort der Legende fest
ax1.set_xticks(np.linspace(-1, 1, 5))
ax1.grid()

# %% [markdown]
# | Location String |  Location Code |
# | :- | :-: |
# |'best'|  0 |
# 'upper right' |    1
# 'upper left'  |   2
# 'lower left'  |  3
# 'lower right' |  4
# 'right'       |  5
# 'center left' |  6
# 'center right'|  7
# 'lower center'|  8
# 'upper center'|  9
# 'center'      |  10

# %%
# help(plt.plot)

# %% [markdown]
# Verwendung logarithmischer Achsen:

# %%
x = np.linspace(1,10,21)
y = 2**(-x)
fig, ax = plt.subplots(1)
ax.semilogy(x, y, '+');

# %% [markdown]
# Siehe auch semilogx() und loglog(), wenn die x-Achse bzw. beide Achsen logarithmisch skaliert werden sollen

# %%
x, y = np.random.rand(500), np.random.randn(500)
fig, (ax1, ax2) = plt.subplots(2)
ax1.scatter(x, y, c=x) # wie plot(), wobei die Punkte unterschiedliche Farben (c) und Größen (s) haben
ax2.scatter(x, y, c=y , s=1+(y-np.mean(y))**2)

# %% [markdown]
# Vektorpfeile mit
#
# __quiver__

# %%
fig, ax = plt.subplots(figsize = (3, 3))
x, y, u, v = 0, 0, 1, 1
ax.quiver(x, y, u, v)
ax.set_title('Quiver mit einem Pfeil')
ax.grid()

# %%
fig, ax = plt.subplots(figsize = (3, 3))
x, y, u, v = 0, 0, 1, 1
ax.quiver(x, y, u, v, angles='xy', scale_units='xy', scale=1)
ax.set_title('Quiver mit einem Pfeil')
ax.axis([-2, 2, -2, 2])
ax.grid()

# %%
fig, ax = plt.subplots(figsize = (3, 3))
n = 7
t = np.linspace(0, 2*np.pi, n)
x = np.sin(t)
y = np.cos(t)
u = np.cos(t)
v = -np.sin(t)
color = t
qv = ax.quiver(x, y, u, v, color, angles='xy', scale_units='xy', scale=1, cmap='plasma')
ax.set_title('Quiver mit vielen Pfeilen')
ax.axis([-2, 2, -2, 2])
fig.colorbar(qv);

# %% [markdown]
# Der Parameter __cmap__ wählt die Colormap aus.  
# Link zu Colormaps (cmap) https://matplotlib.org/stable/tutorials/colors/colormaps.html

# %% [markdown]
# Flächen füllen
#
# __fill__

# %%
fig, ax = plt.subplots(1)
dreieck = [[0,0], [1,0], [0,1], [0,0]]
ax.plot([0,1,0,0], [0,0,1,0])
ax.axis(xmin=-.01,ymin=-.01);

# %%
fig, ax = plt.subplots(1)
dreieck = [[0,0], [1,0], [0,1], [0,0]]
ax.fill([0,1,0,0], [0,0,1,0],color='red')
ax.axis(xmin=-.01,ymin=-.01);

# %%
x = np.linspace(0,1.8*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)

# %%
fig, ax = plt.subplots(1)
ax.fill(x,y1)
ax.plot(x,y1,'xr')
ax.axis('equal')

# %% [markdown]
# Zeichnen von 2d reellwertigen Daten
#
# __imshow__

# %%
M = (-1)**np.arange(25).reshape(5,5)
fig, ax = plt.subplots(1)
ax.imshow(M, cmap='Greys')

# %% [markdown]
# # 3D Plots

# %%
# #%matplotlib qt

# %%
import numpy as np
import matplotlib.pyplot as plt

# %% [markdown]
# Polygonzug in 3d mit  __plot3D__

# %%
x = np.random.rand(5)
y = np.random.rand(5)
z = np.random.randn(5)

ax = plt.figure(10, clear = True).add_subplot(projection='3d')
ax.plot3D(x, y, z, 'co-');

# %% [markdown]
# Fläche in 3d mit __surface__

# %%
from itertools import product, combinations

fig = plt.figure(figsize=(6, 5))

ax = fig.add_subplot(1, 1, 1, projection='3d')

phi = np.linspace(0, 2*np.pi, 12)
phi = np.append(phi, np.nan).reshape(-1, 1) # NaN Trick damit keine ungewollten Linien entstehen
theta = np.linspace(0, np.pi, 12)
theta = np.append(theta, np.nan).reshape(1, -1)

x = np.cos(phi) * np.sin(theta)   # durch broadcasting sind x, y, z hier 2D arrays
y = np.sin(phi) * np.sin(theta)
z = np.ones_like(phi) * np.cos(theta)
# Parametrisierung der Kugelfläche siehehttps://de.wikipedia.org/wiki/Kugel
ax.plot3D(x.flatten(), y.flatten(), z.flatten(), 'r')
#ax.plot3D(x.T.flatten(), y.T.flatten(), z.T.flatten(), 'y')
#ax.plot_surface(x, y, z)

#Wuerfel
r = [-1, 1]
for p1, p2 in combinations(np.array(list(product(r, repeat=3))), 2):
    if np.sum(np.abs(p1-p2)) == r[1]-r[0]:
        ax.plot3D(*zip(p1, p2), color="b")

ax.set_box_aspect((1, 1, 1))

# %% [markdown]
# Vektorpfeile in 3d mit __quiver3D__

# %%
ax.set_xlim(-3, 3)
ax.set_ylim(-3, 3)
ax.set_zlim(-3, 3)

# %%
