%%javascript
// disable scrolling for cells outputs, see https://stackoverflow.com/a/41646403
= function(lines) {
IPython.OutputArea.prototype._should_scroll return false;
}
Matplotlib plots from pandas
import pandas as pd
from matplotlib import pyplot as plt
= pd.read_csv('rates.csv', parse_dates=['Time'])
df df.head()
Time | USD | JPY | BGN | CZK | DKK | GBP | CHF | |
---|---|---|---|---|---|---|---|---|
0 | 2024-01-17 | 1.0877 | 160.65 | 1.9558 | 24.755 | 7.4586 | 0.85818 | 0.9406 |
1 | 2024-01-16 | 1.0882 | 159.64 | 1.9558 | 24.710 | 7.4582 | 0.86078 | 0.9361 |
2 | 2024-01-15 | 1.0945 | 159.67 | 1.9558 | 24.714 | 7.4590 | 0.86075 | 0.9351 |
3 | 2024-01-12 | 1.0942 | 159.17 | 1.9558 | 24.689 | 7.4565 | 0.85950 | 0.9350 |
4 | 2024-01-11 | 1.0987 | 159.71 | 1.9558 | 24.659 | 7.4568 | 0.86145 | 0.9338 |
# here we do not take control over which "ax" object the curves are plotted on
='Time', y='USD', kind='line')
df.plot(x='Time', y='CHF', kind='line') df.plot(x
# here we do not take control over which "ax" object the curves are plotted on
df.USD.hist() df.CHF.hist()
Anatomy of a matplotlib figure
Credits: https://matplotlib.org/stable/gallery/showcase/anatomy.html#anatomy-of-a-figure
Taking control of where to plot curves with subplots
Credits: https://matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/subplots_demo.html
Draw two curves on one ax
= plt.gca() # get the single current ax created by default
my_current_ax ='Time', y='USD', kind='line', ax=my_current_ax)
df.plot(x='Time', y='CHF', kind='line', ax=my_current_ax) df.plot(x
= plt.gca() # get the single current ax created by default
my_current_ax =my_current_ax)
df.USD.hist(ax=my_current_ax) df.CHF.hist(ax
Two horizontal subplots
= plt.subplots(ncols=2)
fig, axs type(axs), len(axs)
=axs[0])
df.USD.hist(ax=axs[1]) df.CHF.hist(ax
='Time', y=['USD', 'CHF'], kind='line') df.plot(x
# from https://matplotlib.org/stable/users/explain/axes/arranging_axes.html#basic-2x2-grid
def annotate_axes(ax, text, fontsize=18):
0.5, 0.5, text, transform=ax.transAxes,
ax.text(="center", va="center", fontsize=fontsize, color="darkgrey") ha
Two by two grid of subplots
# from https://matplotlib.org/stable/users/explain/axes/arranging_axes.html#basic-2x2-grid
= plt.subplots(ncols=2, nrows=2, figsize=(5.5, 3.5),
fig, axs ="constrained")
layout# add an artist, in this case a nice label in the middle...
for row in range(2):
for col in range(2):
f'axs[{row}, {col}]', (0.5, 0.5),
axs[row, col].annotate(=axs[row, col].transAxes,
transform='center', va='center', fontsize=18,
ha='darkgrey')
color='Time', ax=axs[0,0])
df.USD.plot(x='Time', ax=axs[1,1])
df.CHF.plot(x='Time', ax=axs[1,0])
df.BGN.plot(x
'plt.subplots()') fig.suptitle(
Text(0.5, 0.98, 'plt.subplots()')
Mosaic subplots
# from https://matplotlib.org/stable/users/explain/axes/arranging_axes.html#basic-2x2-grid
= plt.subplot_mosaic([['upper left', 'right'],
fig, axd 'lower left', 'right']],
[=(5.5, 3.5), layout="constrained")
figsizefor k, ax in axd.items():
f'axd[{k!r}]')
annotate_axes(ax, 'plt.subplot_mosaic()')
fig.suptitle(=axd['upper left'])
df.USD.plot(ax=axd['right'])
df.USD.hist(ax='Time', y='USD', ax=axd['lower left']) df.plot.scatter(x