Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132923 views
License: OTHER
Kernel: Python 2

matplotlib

Credits: Content forked from Parallel Machine Learning with scikit-learn and IPython by Olivier Grisel

  • Setting Global Parameters

  • Basic Plots

  • Histograms

  • Two Histograms on the Same Plot

  • Scatter Plots

%matplotlib inline import pandas as pd import numpy as np import pylab as plt import seaborn

Setting Global Parameters

# Set the global default size of matplotlib figures plt.rc('figure', figsize=(10, 5)) # Set seaborn aesthetic parameters to defaults seaborn.set()

Basic Plots

x = np.linspace(0, 2, 10) plt.plot(x, x, 'o-', label='linear') plt.plot(x, x ** 2, 'x-', label='quadratic') plt.legend(loc='best') plt.title('Linear vs Quadratic progression') plt.xlabel('Input') plt.ylabel('Output'); plt.show()
Image in a Jupyter notebook

Histograms

# Gaussian, mean 1, stddev .5, 1000 elements samples = np.random.normal(loc=1.0, scale=0.5, size=1000) print(samples.shape) print(samples.dtype) print(samples[:30]) plt.hist(samples, bins=50); plt.show()
(1000,) float64 [ 0.6806888 0.72202042 1.40490113 1.13979846 0.5729488 1.32584077 0.61635621 0.60340336 1.29453467 0.69841457 0.6975998 0.72315991 0.66912189 1.03420801 0.62283168 0.38582511 0.89488414 1.4802518 1.43819256 0.98605861 0.60402232 1.03820507 0.35598796 1.32901087 1.03194436 1.3374366 1.82526334 1.26614489 1.20061661 0.86344001]
Image in a Jupyter notebook

Two Histograms on the Same Plot

samples_1 = np.random.normal(loc=1, scale=.5, size=10000) samples_2 = np.random.standard_t(df=10, size=10000) bins = np.linspace(-3, 3, 50) # Set an alpha and use the same bins since we are plotting two hists plt.hist(samples_1, bins=bins, alpha=0.5, label='samples 1') plt.hist(samples_2, bins=bins, alpha=0.5, label='samples 2') plt.legend(loc='upper left'); plt.show()
Image in a Jupyter notebook

Scatter Plots

plt.scatter(samples_1, samples_2, alpha=0.1); plt.show()
Image in a Jupyter notebook