# 3. Matplotlib Charts¶

## 3.1. Bars¶

• used to display single values
```x = [1,2,3]
y = [4,5,6]

plt.bar(x, y, label='Bars1')
```
```x1 = [2,4,6,8,10]
y1 = [6,7,8,2,4]

x2 = [1,3,5,7,9]
y2 = [7,8,2,4,2]

plt.bar(x1, y1, label='Bars 1', color='blue')
plt.bar(x2, y2, label='Bars 2', color='red')

plt.xlabel('x')
plt.ylabel('y')
plt.title('This is my chart')
plt.legend()
plt.show()
```

Fig. 3.7. Bars

## 3.2. Histogram¶

• used to display number of elements in specific groups
```ages = np.random.randint(size=50, low=0, high=130)

# age groups
bins = [0, 10, 20, 30, 40, 50, 60, 70, 80, 100, 110, 120, 130]

plt.hist(ages, bins, histtype='bar', rwidth=0.8)
```

Fig. 3.8. Histogram

## 3.3. Scatter plot¶

• Used to show correlation
```x = [1,2,3,4,5,6,7,8]
y = [4,7,6,2,7,4,5,2]

plt.scatter(x, y)
```
```x = [1,2,3,4,5,6,7,8]
y = [4,7,6,2,7,4,5,2]

plt.scatter(x, y, marker='*', color='red', s=500)   # ``s`` = size
plt.scatter(y, x, marker='o', color='blue')
```

Fig. 3.9. Scatter plot

## 3.4. Stack plot¶

• Display sections in whole group
```days = [1, 2, 3, 4, 5]
labels = ['To Do', 'In Progress', 'In Test', 'In Review', 'Done']

# how many issues were in each status on given day
todo = [10, 8, 6, 4, 2]
in_progress = [2, 3, 4, 3, 2]
in_test = [7, 8, 7, 2, 2]
in_review = [8, 5, 7, 8, 1]
done = [0, 2, 4, 6, 12]

fig, ax = plt.subplots()
ax.stackplot(days, todo,in_progress,in_test,in_review,done, labels=labels)
ax.legend(loc='upper left')
plt.show()
```

Fig. 3.10. Stack plot

## 3.5. Pie chart¶

• Display sections in whole group

• Automaticly calculate percentage

• optional features:

• slice labels
• auto-labeling the percentage
• offsetting a slice with “explode”
• custom start angle
```import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')

fig1, ax1 = plt.subplots()

ax1.pie(sizes,
explode=explode,
labels=labels,
autopct='%1.1f%%',  # auto percentage
startangle=90)  # at what angle first element starts

ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()
```

Fig. 3.11. Pie chart

## 3.6. Nested Pie Chart¶

```fig, ax = plt.subplots()

size = 0.3
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])

cmap = plt.get_cmap("tab20c")
outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap(np.array([1, 2, 5, 6, 9, 10]))

wedgeprops=dict(width=size, edgecolor='w'))

wedgeprops=dict(width=size, edgecolor='w'))

ax.set(aspect="equal", title='Pie plot with `ax.pie`')
plt.show()
```

Fig. 3.12. Pie chart

## 3.7. 3D¶

### 3.7.1. 3D Sphere¶

Code Listing 3.21. 3D Sphere
```from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()

# Make data
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))

# Plot the surface
ax.plot_surface(x, y, z, color='b')

plt.show()
```

### 3.7.2. 3D Surface¶

Code Listing 3.22. 3D Surface
```from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np

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

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X ** 2 + Y ** 2)
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()
```

## 3.8. Assignments¶

### 3.8.1. Color graph¶

1. Dokonano pomiarów z urządzeń temperatury
2. Wygeneruj listę `dict` z datami z ostatniego miesiąca oraz wartością pomiarów losowo 10-15 plus szum na poziomie 0.5 stopnia celsiusza (wykrzystaj `np.gauss()`)
3. Mając do dyspozycji szereg czasowy, gdzie dla każdego dnia wykonano pomiar temperatury
4. Przedstaw na wykresie dane szeregu czasowego
5. Oś z datami przedstaw przekrzywioną o 45 stopni
6. Na osi y przedstawiaj tylko pełne `int`
7. Dodaj Colorbar ze skalą temperatur zimno-ciepło
8. Użyj kolorów niebieski (zimno), czerwony (ciepło)
9. Wykres ma mieć grid