1 Plotting with Matplotlib
2 ------------------------
4 Also creating a presentation with rst2pdf
5 =========================================
9 Favour simpler data structures if they do what you need. In order:
13 - Can't install system dependencies
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
21 Shamelessly stolen from https://stackoverflow.com/a/45288000
23 Loading Data from Disk
24 ----------------------
28 .. code-block:: python
31 >>> with open('eggs.csv', newline='') as csvfile:
32 ... spam = csv.reader(csvfile,
39 Loading Data from Disk
40 ----------------------
44 .. code-block:: python
47 >>> spam = numpy.genfromtxt('eggs.csv',
49 ... dtype=None) # No error handling!
54 ``numpy.genfromtxt`` will try to infer the datatype of each column if
55 ``dtype=None`` is set.
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)
60 Loading Data from Disk
61 ----------------------
64 **Remind me to look at some actual numpy usage at the end**
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
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
74 Loading Data from Disk
75 ----------------------
79 .. code-block:: python
82 >>> # dtype=None is def
83 >>> spam = pandas.read_csv('eggs.csv',
90 ``header=None`` is required if the flie does not have a header.
94 Generating Data for Testing
95 ---------------------------
97 Generating the data on the fly with numpy is convenient.
99 .. code-block:: python
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))
116 Plot data of the form:
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:
132 .. code-block:: python
134 >>> # Some plotting has previously occured
135 >>> plt.savefig('eggs.pdf', dpi=300, transparent=False)
137 The output format is interpreted from the file extension.
138 The keyword arguments are optional here. Other options exist.
150 NumPy User Guide: https://docs.scipy.org/doc/numpy/user/index.html
152 NumPy Reference: https://docs.scipy.org/doc/numpy/reference/index.html#reference
154 Matplotlib example gallery: https://matplotlib.org/gallery/index.html
156 Pandas: It probably exists. Good luck.
158 This presentation: https://git.friedersdorff.com/max/plotting_with_matplotlib.git