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 ----------------------
65 .. code-block:: python
68 >>> # dtype=None is def
69 >>> spam = pandas.read_csv('eggs.csv',
76 ``header=None`` is required if the flie does not have a header.
80 Generating Data for Testing
81 ---------------------------
83 Generating the data on the fly with numpy is convenient.
85 .. code-block:: python
87 >>> import numpy.random as ran
88 >>> # For repeatability
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))
102 Plot data of the form:
106 .. code-block:: python
107 :include: time_series.py
112 .. code-block:: python
118 So far I've just displayed plots with ``plt.sow()``. You can actually save
119 the plots from that interface manually, but when scripting, it's convenient
120 to do so automatically:
122 .. code-block:: python
124 >>> # Some plotting has previously occured
125 >>> plt.savefig('eggs.pdf', dpi=300, transparent=False)
127 The output format is interpreted from the file extension.
129 The keyword arguments are optional here. Other options exist.
137 .. code-block:: python
138 :include: stacked_bars.py