1
+ """
2
+ ---"subplot" syntax (Check around line 1411 in pyplot.py)---
3
+
4
+ @_docstring.interpd
5
+ def subplot(*args, **kwargs) -> Axes:
6
+
7
+ 1. The "->" syntax stands for a function annotation: it has been introduced with Python3 and it only documents the various
8
+ types in our code, but it has no effects at runtime (not slower, not faster). I can write whatever I want without
9
+ influence my code.
10
+
11
+ Example #1:
12
+ def sum(x: int, y: int) -> int:
13
+ return x + y
14
+
15
+ Here I'm saying to the user that "x" should be an integer as well as "y" and that the return value will be an integer too.
16
+ So if I run sum(1, 2) I'll get 3.
17
+
18
+ If now I change my function definition into:
19
+ def sum(x: str, y: int) -> str:
20
+ return x + y
21
+
22
+ Here I'm saying to the user that "x" as well as the return value should be a string while "y" should be an integer.
23
+ If I run sum(1, 2) I'll get another time 3, and this because I have only made a function annotation that doesn't
24
+ influence at all during the runtime. Thus, Python doesn't interpret a string as "x" only because I've written "x: str".
25
+
26
+ To access function annotaions I can type:
27
+ sum.__annotation__
28
+
29
+ While to access function documentation (that is explicited into """ """ after the "def" keyword) I can type:
30
+ sum.__doc__
31
+
32
+ Check the links below for more info:
33
+ https://www.youtube.com/watch?v=k56rEoRjK4k&t=192s
34
+ https://peps.python.org/pep-3107/
35
+
36
+ 2. subplot(nrows, ncols, index). The subplot will take the "index" position on a grid with "nrows" rows and *ncols* columns.
37
+ "index" starts at 1 in the upper left corner and increases to the right.
38
+
39
+ Example #1:
40
+ nrows = 2
41
+ ncols = 1
42
+ index = 1
43
+ I'm creating a matrix with 2 rows and 1 col and my plot is going to occupy the upper half of the figure.
44
+
45
+ Example #2:
46
+ nrows = 3
47
+ ncols = 1
48
+ index = (1,2) -> "index" is a two-touple (a Python's touple is a sort of Matlab's cell) this time...
49
+ Here I'm creating a plot that is going to occupty the uppermost 2/3 of the figure. If "index" is a two-touple
50
+ element, the first touple's element indicates the starting index in the grid, while the second one the lasting.
51
+
52
+ 3. subplot(211) = subplot(2, 1, 1)
53
+ """
54
+
55
+ import matplotlib
56
+ import matplotlib .pyplot as plt
57
+ import numpy as np
58
+
59
+ """
60
+ figure = plt.figure()
61
+ ax = figure.add_subplot(111)
62
+ ax2 = ax.twinx()
63
+ ax2.cla()#or clear() ; doesn't work both ways
64
+ ax.set_ylabel('ax_label')
65
+ ax2.set_ylabel('ax2_label')#expected to be on the right
66
+ plt.show()
67
+ """
68
+
69
+ """
70
+ fig, ax1 = plt.subplots()
71
+ ax2 = ax1.twinx()
72
+ for i in range(2):
73
+ # do some plotting here
74
+ ax2.set(ylabel=f'ylabel of plot # {i}', ylim=(1, i + 2))
75
+ ax2.yaxis.set_label_position('right')
76
+ fig.savefig(f'{i}.png')
77
+ ax1.cla()
78
+ ax2.cla()
79
+ """
80
+
81
+ ax0 = plt .subplot (211 ) # subplot(211) = subplot(2,1,1) ->
82
+ ax1 = plt .subplot (212 , sharex = ax0 )
83
+ ax0 .plot (range (100 ))
84
+ ax1 .plot (range (100 ))
85
+ ax0 .cla ()
86
+ plt .show ()
0 commit comments