]> git.friedersdorff.com Git - max/rst2pdf_presentation.git/blob - slides.rst
Add some space before images
[max/rst2pdf_presentation.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 Numpy NB.
63 =========
64 **Remind me to look at some actual numpy usage at the end**
65
66 - I think numpy does some type coercion when creating arrays.
67 - Arrays created by ``numpy.genfromtxt`` can not in general be indexed like
68   ``data[xstart:xend, ystart:yend]``.
69 - Data of unequal types are problematic!  Pandas *may* be a better choice in
70   that case.
71 - Specifying some value for ``dtype`` is probably necessary in most cases in
72   practice: https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
73
74 Loading Data from Disk
75 ----------------------
76 Pandas
77 ======
78
79 .. code-block:: python
80
81    >>> import pandas
82    >>> # dtype=None is def
83    >>> spam = pandas.read_csv('eggs.csv',
84    ...                        delimiter=' ',
85    ...                        header=None) 
86    >>> for row in spam:
87    ...     # Do things
88    ...     pass
89
90 ``header=None`` is required if the flie does not have a header.
91
92
93
94 Generating Data for Testing
95 ---------------------------
96
97 Generating the data on the fly with numpy is convenient.
98
99 .. code-block:: python
100
101    >>> import numpy.random as ran
102    >>> # For repeatability 
103    >>> ran.seed(7890234) 
104    >>> # Uniform [0, 1) floats
105    >>> data = ran.rand(100, 2)
106    >>> # Uniform [0, 1) floats
107    >>> data = ran.rand(100, 100, 100)
108    >>> # Std. normal floats
109    >>> data = ran.randn(100)
110    >>> # 3x14x15 array of binomial ints with n = 100, p = 0.1
111    >>> data = ran.binomial(100, 0.1, (3, 14, 15))
112
113 Plotting Time Series
114 --------------------
115
116 Plot data of the form:
117
118 .. math:: y=f(t)
119
120
121 Subplots
122 --------
123
124
125 Saving Plots
126 ------------
127
128 So far I've just displayed plots with ``plt.show()``.  You can actually save 
129 the plots from that interface manually, but when scripting, it's convenient
130 to do so automatically:
131
132 .. code-block:: python
133    
134    >>> # Some plotting has previously occured
135    >>> plt.savefig('eggs.pdf', dpi=300, transparent=False)
136
137 The output format is interpreted from the file extension.  
138 The keyword arguments are optional here.  Other options exist.
139
140 Error Bars
141 ----------
142
143
144 Stacked Bar Graph
145 -----------------
146
147
148 Resources
149 ---------
150 NumPy User Guide: https://docs.scipy.org/doc/numpy/user/index.html
151
152 NumPy Reference: https://docs.scipy.org/doc/numpy/reference/index.html#reference
153
154 Matplotlib example gallery: https://matplotlib.org/gallery/index.html
155
156 Pandas: It probably exists.  Good luck.
157
158 This presentation: https://git.friedersdorff.com/max/plotting_with_matplotlib.git