3 Practical Programming

Practical programming


%timeit # ipython magic function
%matplotlib inline # ipython magic function
import numpy as np
import matplotlib.pyplot as plt

# normal distribution random varibale
np.random.normal()

# 

# histogram
plt.hist(a, bins = 1)

# typical plotting
fig1, ax1 = plt.subplots()
fig1.set_size_inches(16,4)
for datum in [datum1, datum2]
    ax1.plot(x, datum, label = '{} periods'.format(1))
ax1.set_xlabel('Time / s')
ax1.set_ylabel('angular displacement / rad')
ax1.set_title('Numerically integrated oscillations of the free pendulum over time')
ax1.legend()

# supress cell printed output by
;

# seaborn package for heatmap
import seaborn as sns
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, linewidth=0.5)
plt.show()

corr = np.corrcoef(np.random.randn(10, 200))
mask = np.zeros_like(corr)
mask[np.triu_indices_from(mask)] = True
with sns.axes_style("white"):
    ax = sns.heatmap(corr, mask=mask, vmax=.3, square=True,  cmap="YlGnBu")
    plt.show()

# or np imshow for heatmap
def heatmap2d(arr: np.ndarray):
    plt.imshow(arr, cmap='viridis')
    plt.colorbar()
    plt.show()

test_array = np.arange(100 * 100).reshape(100, 100)
heatmap2d(test_array)