]> git.friedersdorff.com Git - max/plotting_with_matplotlib.git/blob - slides.rst
feat: Add code to generate python subplot
[max/plotting_with_matplotlib.git] / slides.rst
1 Plotting with Matplotlib
2 ------------------------
3
4 Also creating a presentation with rst2pdf
5 =========================================
6
7 Data Structures
8 ---------------
9 Favour simpler data structures if they do what you need.  In order:
10
11 #. Built-in Lists
12     - 2xN data or simpler
13     - Can't install system dependencies
14 #. Numpy arrays
15     - 2 (or higher) dimensional data
16     - Lots of numerical calculations
17 #. Pandas series/dataframes
18     - 'Data Wrangling', reshaping, merging, sorting, querying
19     - Importing from complex formats
20
21 Shamelessly stolen from https://stackoverflow.com/a/45288000
22
23 Loading Data from Disk
24 ----------------------
25 Natively
26 ========
27
28 .. code-block:: python
29
30    >>> import csv
31    >>> with open('eggs.csv', newline='') as csvfile:
32    ...     spam = csv.reader(csvfile, 
33    ...                       delimiter=' ', 
34    ...                       quotechar='|')
35    ...     for row in spam:
36    ...         # Do things
37    ...         pass
38
39 Loading Data from Disk
40 ----------------------
41 Numpy
42 =====
43
44 .. code-block:: python
45
46    >>> import numpy
47    >>> spam = numpy.genfromtxt('eggs.csv', 
48    ...                         delimiter=' ', 
49    ...                         dtype=None) # No error handling!
50    >>> for row in spam:
51    ...     # Do things
52    ...     pass
53
54 ``numpy.genfromtxt`` will try to infer the datatype of each column if 
55 ``dtype=None`` is set.
56
57 ``numpy.loadtxt`` is generally faster at runtime if your data is well formated 
58 (no missing values, only numerical data or constant length strings)
59
60 Loading Data from Disk
61 ----------------------
62 Pandas
63 ======
64
65 .. code-block:: python
66
67    >>> import pandas
68    >>> # dtype=None is def
69    >>> spam = pandas.read_csv('eggs.csv',
70    ...                        delimiter=' ',
71    ...                        header=None) 
72    >>> for row in spam:
73    ...     # Do things
74    ...     pass
75
76 ``header=None`` is required if the flie does not have a header.
77
78
79
80 Generating Data for Testing
81 ---------------------------
82
83 Generating the data on the fly with numpy is convenient.
84
85 .. code-block:: python
86
87    >>> import numpy.random as ran
88    >>> # For repeatability 
89    >>> ran.seed(7890234) 
90    >>> # Uniform [0, 1) floats
91    >>> data = ran.rand(100, 2)
92    >>> # Uniform [0, 1) floats
93    >>> data = ran.rand(100, 100, 100)
94    >>> # Std. normal floats
95    >>> data = ran.randn(100)
96    >>> # 3x14x15 array of binomial ints with n = 100, p = 0.1
97    >>> data = ran.binomial(100, 0.1, (3, 14, 15))
98
99 Plotting Time Series
100 --------------------
101
102 Plot data of the form:
103
104 .. math:: y=f(t)
105
106 .. code-block:: python
107
108    >>> import matplotlib.pyplot as plt
109    >>>
110    >>> t = range(50)
111    >>> x = (ran.rand(50)*50) + 2000 # I don't have real data
112    >>> plt.plot(t, x)
113    >>> plt.title('Some time series with left title', loc='left')
114    >>> plt.ylabel('Mass of test mass over time')
115    >>> plt.show()