SENCE 2024 Examples¶
Sankey with different colors¶
Find color combinations at https://designwizard.com/blog/colour-combination/#gray-ff-and-lime-punch-dedff
In [1]:
Copied!
import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Two Systems")
flows = [0.25, 0.15, 0.60, -0.10, -0.05, -0.25, -0.15, -0.10, -0.35]
sankey = Sankey(ax=ax, unit=None)
sankey.add(flows=flows, label='one',
orientations=[-1, 1, 0, 1, 1, 1, -1, -1, 0],
facecolor='#606060FF')
sankey.add(flows=[-0.25, 0.15, 0.1], label='two',
orientations=[-1, -1, -1], prior=0, connect=(0, 0),
facecolor='#D6ED17FF')
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend();
import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Two Systems")
flows = [0.25, 0.15, 0.60, -0.10, -0.05, -0.25, -0.15, -0.10, -0.35]
sankey = Sankey(ax=ax, unit=None)
sankey.add(flows=flows, label='one',
orientations=[-1, 1, 0, 1, 1, 1, -1, -1, 0],
facecolor='#606060FF')
sankey.add(flows=[-0.25, 0.15, 0.1], label='two',
orientations=[-1, -1, -1], prior=0, connect=(0, 0),
facecolor='#D6ED17FF')
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend();
Read online CSV¶
In [2]:
Copied!
import pandas as pd
import pandas as pd
In [3]:
Copied!
pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv")
pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv")
Out[3]:
#group | false | false.1 | true | true.1 | false.2 | false.3 | true.2 | true.3 | |
---|---|---|---|---|---|---|---|---|---|
0 | #datatype | string | long | dateTime:RFC3339 | dateTime:RFC3339 | dateTime:RFC3339 | double | string | string |
1 | #default | mean | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2 | NaN | result | table | _start | _stop | _time | _value | _field | _measurement |
3 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-04-01T07:38:29.058Z | 9.200975609756101 | value | wetterstation.temperatur |
4 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-04-01T22:42:28.424Z | 8.58029850746268 | value | wetterstation.temperatur |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
359 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-11-10T19:18:43.354Z | 3.659710144927535 | value | wetterstation.temperatur |
360 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-11-11T10:22:42.72Z | 1.9895384615384597 | value | wetterstation.temperatur |
361 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-11-12T01:26:42.086Z | 5.282580645161291 | value | wetterstation.temperatur |
362 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-11-12T16:30:41.452Z | 4.560792079207922 | value | wetterstation.temperatur |
363 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-11-12T21:56:11.652Z | 2.4579310344827574 | value | wetterstation.temperatur |
364 rows × 9 columns
In [4]:
Copied!
pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv",
skiprows=3)
pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv",
skiprows=3)
Out[4]:
Unnamed: 0 | result | table | _start | _stop | _time | _value | _field | _measurement | |
---|---|---|---|---|---|---|---|---|---|
0 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-04-01T07:38:29.058Z | 9.200976 | value | wetterstation.temperatur |
1 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-04-01T22:42:28.424Z | 8.580299 | value | wetterstation.temperatur |
2 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-04-02T13:46:27.79Z | 8.436757 | value | wetterstation.temperatur |
3 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-04-03T04:50:27.156Z | 6.948889 | value | wetterstation.temperatur |
4 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-04-03T19:54:26.522Z | 9.091223 | value | wetterstation.temperatur |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
356 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-11-10T19:18:43.354Z | 3.659710 | value | wetterstation.temperatur |
357 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-11-11T10:22:42.72Z | 1.989538 | value | wetterstation.temperatur |
358 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-11-12T01:26:42.086Z | 5.282581 | value | wetterstation.temperatur |
359 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-11-12T16:30:41.452Z | 4.560792 | value | wetterstation.temperatur |
360 | NaN | NaN | 0 | 2024-03-31T22:00:00Z | 2024-11-12T21:56:11.652Z | 2024-11-12T21:56:11.652Z | 2.457931 | value | wetterstation.temperatur |
361 rows × 9 columns
In [5]:
Copied!
pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv",
skiprows=3,
parse_dates=[3, 4, 5])
pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv",
skiprows=3,
parse_dates=[3, 4, 5])
Out[5]:
Unnamed: 0 | result | table | _start | _stop | _time | _value | _field | _measurement | |
---|---|---|---|---|---|---|---|---|---|
0 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2024-04-01 07:38:29.058000+00:00 | 9.200976 | value | wetterstation.temperatur |
1 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2024-04-01 22:42:28.424000+00:00 | 8.580299 | value | wetterstation.temperatur |
2 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2024-04-02 13:46:27.790000+00:00 | 8.436757 | value | wetterstation.temperatur |
3 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2024-04-03 04:50:27.156000+00:00 | 6.948889 | value | wetterstation.temperatur |
4 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2024-04-03 19:54:26.522000+00:00 | 9.091223 | value | wetterstation.temperatur |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
356 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2024-11-10 19:18:43.354000+00:00 | 3.659710 | value | wetterstation.temperatur |
357 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2024-11-11 10:22:42.720000+00:00 | 1.989538 | value | wetterstation.temperatur |
358 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2024-11-12 01:26:42.086000+00:00 | 5.282581 | value | wetterstation.temperatur |
359 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2024-11-12 16:30:41.452000+00:00 | 4.560792 | value | wetterstation.temperatur |
360 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2.457931 | value | wetterstation.temperatur |
361 rows × 9 columns
In [6]:
Copied!
pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv",
skiprows=3,
parse_dates=[3, 4, 5],
index_col='_time'
)
pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv",
skiprows=3,
parse_dates=[3, 4, 5],
index_col='_time'
)
Out[6]:
Unnamed: 0 | result | table | _start | _stop | _value | _field | _measurement | |
---|---|---|---|---|---|---|---|---|
_time | ||||||||
2024-04-01 07:38:29.058000+00:00 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 9.200976 | value | wetterstation.temperatur |
2024-04-01 22:42:28.424000+00:00 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 8.580299 | value | wetterstation.temperatur |
2024-04-02 13:46:27.790000+00:00 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 8.436757 | value | wetterstation.temperatur |
2024-04-03 04:50:27.156000+00:00 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 6.948889 | value | wetterstation.temperatur |
2024-04-03 19:54:26.522000+00:00 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 9.091223 | value | wetterstation.temperatur |
... | ... | ... | ... | ... | ... | ... | ... | ... |
2024-11-10 19:18:43.354000+00:00 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 3.659710 | value | wetterstation.temperatur |
2024-11-11 10:22:42.720000+00:00 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 1.989538 | value | wetterstation.temperatur |
2024-11-12 01:26:42.086000+00:00 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 5.282581 | value | wetterstation.temperatur |
2024-11-12 16:30:41.452000+00:00 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 4.560792 | value | wetterstation.temperatur |
2024-11-12 21:56:11.652000+00:00 | NaN | NaN | 0 | 2024-03-31 22:00:00+00:00 | 2024-11-12 21:56:11.652000+00:00 | 2.457931 | value | wetterstation.temperatur |
361 rows × 8 columns
In [7]:
Copied!
pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv",
skiprows=3,
usecols=['_time', '_value'],
parse_dates=[0],
index_col='_time',
)
pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv",
skiprows=3,
usecols=['_time', '_value'],
parse_dates=[0],
index_col='_time',
)
Out[7]:
_value | |
---|---|
_time | |
2024-04-01 07:38:29.058000+00:00 | 9.200976 |
2024-04-01 22:42:28.424000+00:00 | 8.580299 |
2024-04-02 13:46:27.790000+00:00 | 8.436757 |
2024-04-03 04:50:27.156000+00:00 | 6.948889 |
2024-04-03 19:54:26.522000+00:00 | 9.091223 |
... | ... |
2024-11-10 19:18:43.354000+00:00 | 3.659710 |
2024-11-11 10:22:42.720000+00:00 | 1.989538 |
2024-11-12 01:26:42.086000+00:00 | 5.282581 |
2024-11-12 16:30:41.452000+00:00 | 4.560792 |
2024-11-12 21:56:11.652000+00:00 | 2.457931 |
361 rows × 1 columns
In [8]:
Copied!
df = pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv",
skiprows=3,
usecols=['_time', '_value'],
parse_dates=[0],
index_col='_time',
)
df = df.rename(columns={'_value': 'temperature'})
df
df = pd.read_csv("https://python.ericduminil.com/files/wetterstation.temp.csv",
skiprows=3,
usecols=['_time', '_value'],
parse_dates=[0],
index_col='_time',
)
df = df.rename(columns={'_value': 'temperature'})
df
Out[8]:
temperature | |
---|---|
_time | |
2024-04-01 07:38:29.058000+00:00 | 9.200976 |
2024-04-01 22:42:28.424000+00:00 | 8.580299 |
2024-04-02 13:46:27.790000+00:00 | 8.436757 |
2024-04-03 04:50:27.156000+00:00 | 6.948889 |
2024-04-03 19:54:26.522000+00:00 | 9.091223 |
... | ... |
2024-11-10 19:18:43.354000+00:00 | 3.659710 |
2024-11-11 10:22:42.720000+00:00 | 1.989538 |
2024-11-12 01:26:42.086000+00:00 | 5.282581 |
2024-11-12 16:30:41.452000+00:00 | 4.560792 |
2024-11-12 21:56:11.652000+00:00 | 2.457931 |
361 rows × 1 columns
In [9]:
Copied!
df.plot();
df.plot();
In [10]:
Copied!
df.resample('1W').mean().plot();
df.resample('1W').mean().plot();
Include image in Notebook¶
![Minion](http://octodex.github.com/images/daftpunktocat-thomas.gif)
Create output/ folder if needed¶
In [12]:
Copied!
from pathlib import Path
from pathlib import Path
In [13]:
Copied!
Path('output').mkdir(exist_ok=True)
Path('output').mkdir(exist_ok=True)
2-D Density Plot¶
In [14]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde as kde
# Create data: 200 points
data = np.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 3]], 200)
x, y = data.T
# Create a figure with 6 plot areas
fig, axes = plt.subplots(ncols=6, nrows=1, figsize=(21, 5))
# Everything starts with a Scatterplot
axes[0].set_title('Scatterplot')
axes[0].plot(x, y, 'ko')
# Thus we can cut the plotting window in several hexbins
nbins = 20
axes[1].set_title('Hexbin')
axes[1].hexbin(x, y, gridsize=nbins, cmap=plt.cm.BuGn_r)
# 2D Histogram
axes[2].set_title('2D Histogram')
axes[2].hist2d(x, y, bins=nbins, cmap=plt.cm.BuGn_r)
# Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
k = kde(data.T)
xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))
# plot a density
axes[3].set_title('Calculate Gaussian KDE')
axes[3].pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=plt.cm.BuGn_r)
# add shading
axes[4].set_title('2D Density with shading')
axes[4].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.BuGn_r)
# contour
axes[5].set_title('Contour')
axes[5].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.BuGn_r)
axes[5].contour(xi, yi, zi.reshape(xi.shape) );
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde as kde
# Create data: 200 points
data = np.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 3]], 200)
x, y = data.T
# Create a figure with 6 plot areas
fig, axes = plt.subplots(ncols=6, nrows=1, figsize=(21, 5))
# Everything starts with a Scatterplot
axes[0].set_title('Scatterplot')
axes[0].plot(x, y, 'ko')
# Thus we can cut the plotting window in several hexbins
nbins = 20
axes[1].set_title('Hexbin')
axes[1].hexbin(x, y, gridsize=nbins, cmap=plt.cm.BuGn_r)
# 2D Histogram
axes[2].set_title('2D Histogram')
axes[2].hist2d(x, y, bins=nbins, cmap=plt.cm.BuGn_r)
# Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
k = kde(data.T)
xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))
# plot a density
axes[3].set_title('Calculate Gaussian KDE')
axes[3].pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=plt.cm.BuGn_r)
# add shading
axes[4].set_title('2D Density with shading')
axes[4].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.BuGn_r)
# contour
axes[5].set_title('Contour')
axes[5].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.BuGn_r)
axes[5].contour(xi, yi, zi.reshape(xi.shape) );