-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtutorial_plotting.ipyml
More file actions
213 lines (144 loc) · 5.61 KB
/
tutorial_plotting.ipyml
File metadata and controls
213 lines (144 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
cells:
- markdown: |
# Plotting
- markdown: |
Import the LArray library:
- code: |
from larray import *
- markdown: |
Import the test array `population` from the `demography_eurostat` dataset:
- code: |
demography_eurostat = load_example_data('demography_eurostat')
population = demography_eurostat.population / 1_000_000
# show the 'population' array
population
- markdown: |
Inline matplotlib (required in notebooks):
- code: |
%matplotlib inline
- markdown: |
## Python Plot (matplotlib)
- markdown: |
In a Python script, add the following import on top of the script:
- code: |
import matplotlib.pyplot as plt
- markdown: |
Create and show a simple plot (last axis define the different curves to draw):
- code: |
population['Belgium'].plot()
# shows the figure
plt.show()
- markdown: |
- Create a Line plot with grid, title, label on y axis and user-defined xticks.
- Save the plot as a png file (using `plt.savefig()`).
- Show the plot:
- code: |
population['Belgium'].plot(grid=True, xticks=population.time, title='Belgium')
# add a label aling the y axis
plt.ylabel('population (millions)')
# saves figure in a file (see matplotlib.pyplot.savefig documentation for more details)
plt.savefig('Belgium_population.png')
# WARNING: show() reset the current figure after showing it! Do not call it before savefig
plt.show()
- markdown: |
Specify line styles and width:
- code: |
# line styles: '-' for solid line, '--' for dashed line, '-.' for dash-dotted line and ':' for dotted line
population['Male'].plot(style=['-', '--', '-.'], linewidth=2, xticks=population.time, title='Male')
plt.ylabel('population (millions)')
plt.show()
- markdown: |
Move the legend inside the graph (using `plt.legend(loc='position')`):
- code: |
population['Belgium'].plot(xticks=population.time, title='Male')
plt.ylabel('population (millions)')
# available values for loc are:
# 'best' (default), 'upper right', 'upper left', 'lower left', 'lower right', 'right',
# center left', 'center right', 'lower center', 'upper center', 'center'
plt.legend(loc='lower right')
plt.show()
- markdown: |
Put the legend outside the graph (using `plt.legend(bbox_to_anchor=(x, y))`):
- code: |
population['Belgium'].plot(xticks=population.time, title='Male')
plt.ylabel('population (millions)')
plt.legend(bbox_to_anchor=(1.25, 0.6))
plt.show()
- markdown: |
Create a Bar plot:
- code: |
population['Belgium'].plot.bar(title='Belgium')
plt.ylabel('population (millions)')
plt.legend(bbox_to_anchor=(1.25, 0.6))
plt.show()
- markdown: |
Create a _stacked_ Bar plot:
- code: |
population['Belgium'].plot.bar(title='Belgium', stacked=True)
plt.ylabel('population (millions)')
plt.legend(bbox_to_anchor=(1.25, 0.6))
plt.show()
- markdown: |
Create a multiplot figure (using `plt.subplot(nrows,ncols,index)`):
- code: |
figure, axes = plt.subplots(nrows=len(population.country), ncols=1, sharex=True, figsize=(5, 15))
for row, c in enumerate(population.country):
population[c].plot(ax=axes[row], title=str(c))
plt.ylabel('population (millions)')
plt.xticks(population.time)
plt.show()
- markdown: |
See [plot](../_generated/larray.Array.plot.rst#larray.Array.plot) for more details and examples.
See [pyplot tutorial](https://matplotlib.org/tutorials/introductory/pyplot.html) for a short introduction to `matplotlib.pyplot`.
- markdown: |
## Excel Plot
- markdown: |
It is possible to dump arrays and to make plots in the same time in an Excel workbook:
- markdown: |
```python
# to create a new Excel file, argument overwrite_file must be set to True
with open_excel('workbook_with_plots.xlsx', overwrite_file=True) as wb:
# ---- dump data
# add a new sheet 'BFG' and dump the data for Belgium in it
wb['BFG'] = population['Belgium'].dump()
# store the BFG sheet in a local variable
sh = wb['BFG']
# dump the data for France using the equivalent method add_table()
sh['A18'].add_table(population['France'])
# dump the data for Germany
sh['A35'].add_table(population['Germany'])
# ---- use data to create plots
# basic plot anchored to cell H1 using data for Belgium
sh['H1'].add_plot('A1', title='Belgium population')
# basic plot anchored to cell H18 using data for the France for years 2013 to 2016
sh['H18'].add_plot('A18:E20', title='France population')
# plot with options anchored to cell H35 using data for Germany
sh["H35"].add_plot("A35", title="Germany population", width=500, height=300,
xticks_spacing=2, min_y=40_000_000, max_y=41_500_000)
# actually write data and plots in the Workbook
wb.save()
# the Workbook is automatically closed when getting out the block defined by the with statement
```
# The lines below here may be deleted if you do not need them.
# ---------------------------------------------------------------------------
metadata:
celltoolbar: Edit Metadata
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.9.5
livereveal:
autolaunch: false
scroll: true
nbformat: 4
nbformat_minor: 2