You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contrib/plotting-visualization/matplotlib-line-plot.md
+33-23
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,27 @@
1
1
# Line Chart in Matplotlib
2
2
3
3
A line chart is a simple way to visualize data where we connect individual data points. It helps us to see trends and patterns over time or across categories.
4
-
<br> This type of chart is particularly useful for: </br>
5
-
* Comparing Data: Comparing multiple datasets on the same axes.
6
-
* Highlighting Changes: Illustrating changes and patterns in data.
7
-
* Visualizing Trends: Showing trends over time or other continuous variables.
4
+
5
+
This type of chart is particularly useful for:
6
+
- Comparing Data: Comparing multiple datasets on the same axes.
7
+
- Highlighting Changes: Illustrating changes and patterns in data.
8
+
- Visualizing Trends: Showing trends over time or other continuous variables.
8
9
9
10
## Prerequisites
10
-
Line plots can be created in Python with Matplotlib's ``pyplot`` library. To build a line plot, first import ``Matplotlib``. It is a standard convention to import Matplotlib's pyplot library as ``plt``.
11
-
```
12
-
import matplotlib.pyplot as plt
13
11
12
+
Line plots can be created in Python with Matplotlib's `pyplot` library. To build a line plot, first import `matplotlib`. It is a standard convention to import Matplotlib's pyplot library as `plt`.
13
+
14
+
```python
15
+
import matplotlib.pyplot as plt
14
16
```
15
17
16
18
## Creating a simple Line Plot
17
19
18
20
First import matplotlib and numpy, these are useful for charting.
19
-
<br> You can use the ``plot(x,y)`` method to create a line chart.</br>
20
21
21
-
```
22
+
You can use the `plot(x,y)` method to create a line chart.
23
+
24
+
```python
22
25
import matplotlib.pyplot as plt
23
26
import numpy as np
24
27
@@ -29,15 +32,17 @@ y = 2*x + 1
29
32
plt.plot(x, y)
30
33
plt.show()
31
34
```
35
+
32
36
When executed, this will show the following line plot:
33
37
34
38

35
39
36
40
37
41
## Curved line
38
42
39
-
The ``plot()`` method also works for other types of line charts. It doesn’t need to be a straight line, y can have any type of values.
40
-
```
43
+
The `plot()` method also works for other types of line charts. It doesn’t need to be a straight line, y can have any type of values.
44
+
45
+
```python
41
46
import matplotlib.pyplot as plt
42
47
import numpy as np
43
48
@@ -47,15 +52,17 @@ y = 2**x + 1
47
52
plt.plot(x, y)
48
53
plt.show()
49
54
```
55
+
50
56
When executed, this will show the following Curved line plot:
51
57
52
58

53
59
54
60
55
61
## Line with Labels
56
62
57
-
To know what you are looking at, you need meta data. Labels are a type of meta data. They show what the chart is about. The chart has an ``x label``, ``y label`` and ``title``.
58
-
```
63
+
To know what you are looking at, you need meta data. Labels are a type of meta data. They show what the chart is about. The chart has an `x label`, `y label` and `title`.
64
+
65
+
```python
59
66
import matplotlib.pyplot as plt
60
67
import numpy as np
61
68
@@ -72,15 +79,16 @@ plt.title("With Labels")
72
79
73
80
plt.show()
74
81
```
82
+
75
83
When executed, this will show the following line with labels plot:
76
84
77
85

78
86
79
87
## Multiple lines
80
88
81
-
More than one line can be in the plot. To add another line, just call the ``plot(x,y)`` function again. In the example below we have two different values for ``y(y1,y2)`` that are plotted onto the chart.
89
+
More than one line can be in the plot. To add another line, just call the `plot(x,y)` function again. In the example below we have two different values for `y(y1,y2)` that are plotted onto the chart.
82
90
83
-
```
91
+
```python
84
92
import matplotlib.pyplot as plt
85
93
import numpy as np
86
94
@@ -98,16 +106,17 @@ plt.plot(x, y1,
98
106
99
107
plt.show()
100
108
```
109
+
101
110
When executed, this will show the following Multiple lines plot:
102
111
103
112

104
113
105
114
106
115
## Dotted line
107
116
108
-
Lines can be in the form of dots like the image below. Instead of calling ``plot(x,y)`` call the ``scatter(x,y)`` method. The ``scatter(x,y)`` method can also be used to (randomly) plot points onto the chart.
117
+
Lines can be in the form of dots like the image below. Instead of calling `plot(x,y)` call the `scatter(x,y)` method. The `scatter(x,y)` method can also be used to (randomly) plot points onto the chart.
109
118
110
-
```
119
+
```python
111
120
import matplotlib.pyplot as plt
112
121
import numpy as np
113
122
@@ -130,9 +139,9 @@ When executed, this will show the following Dotted line plot:
130
139
131
140
## Line ticks
132
141
133
-
You can change the ticks on the plot. Set them on the ``x-axis``, ``y-axis`` or even change their color. The line can be more thick and have an alpha value.
142
+
You can change the ticks on the plot. Set them on the `x-axis`, `y-axis` or even change their color. The line can be more thick and have an alpha value.
134
143
135
-
```
144
+
```python
136
145
import matplotlib.pyplot as plt
137
146
import numpy as np
138
147
@@ -166,9 +175,9 @@ When executed, this will show the following line ticks plot:
166
175
167
176
## Line with asymptote
168
177
169
-
An asymptote can be added to the plot. To do that, use ``plt.annotate()``. There’s lso a dotted line in the plot below. You can play around with the code to see how it works.
178
+
An asymptote can be added to the plot. To do that, use `plt.annotate()`. There’s lso a dotted line in the plot below. You can play around with the code to see how it works.
170
179
171
-
```
180
+
```python
172
181
import matplotlib.pyplot as plt
173
182
import numpy as np
174
183
@@ -222,9 +231,9 @@ When executed, this will show the following Line with asymptote plot:
222
231
223
232
## Line with text scale
224
233
225
-
It doesn’t have to be a numeric scale. The scale can also contain textual words like the example below. In ``plt.yticks()`` we just pass a list with text values. These values are then show against the ``y axis``.
234
+
It doesn’t have to be a numeric scale. The scale can also contain textual words like the example below. In `plt.yticks()` we just pass a list with text values. These values are then show against the `y axis`.
0 commit comments