Skip to content

Commit 8392117

Browse files
authored
Update matplotlib-line-plot.md
1 parent 34d7bc3 commit 8392117

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

contrib/plotting-visualization/matplotlib-line-plot.md

+33-23
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
# Line Chart in Matplotlib
22

33
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.
89

910
## 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
1311

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
1416
```
1517

1618
## Creating a simple Line Plot
1719

1820
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>
2021

21-
```
22+
You can use the `plot(x,y)` method to create a line chart.
23+
24+
```python
2225
import matplotlib.pyplot as plt
2326
import numpy as np
2427

@@ -29,15 +32,17 @@ y = 2*x + 1
2932
plt.plot(x, y)
3033
plt.show()
3134
```
35+
3236
When executed, this will show the following line plot:
3337

3438
![Basic line Chart](images/simple_line.png)
3539

3640

3741
## Curved line
3842

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
4146
import matplotlib.pyplot as plt
4247
import numpy as np
4348

@@ -47,15 +52,17 @@ y = 2**x + 1
4752
plt.plot(x, y)
4853
plt.show()
4954
```
55+
5056
When executed, this will show the following Curved line plot:
5157

5258
![Curved line](images/line-curve.png)
5359

5460

5561
## Line with Labels
5662

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
5966
import matplotlib.pyplot as plt
6067
import numpy as np
6168

@@ -72,15 +79,16 @@ plt.title("With Labels")
7279

7380
plt.show()
7481
```
82+
7583
When executed, this will show the following line with labels plot:
7684

7785
![line with labels](images/line-labels.png)
7886

7987
## Multiple lines
8088

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.
8290

83-
```
91+
```python
8492
import matplotlib.pyplot as plt
8593
import numpy as np
8694

@@ -98,16 +106,17 @@ plt.plot(x, y1,
98106

99107
plt.show()
100108
```
109+
101110
When executed, this will show the following Multiple lines plot:
102111

103112
![multiple lines](images/two-lines.png)
104113

105114

106115
## Dotted line
107116

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.
109118

110-
```
119+
```python
111120
import matplotlib.pyplot as plt
112121
import numpy as np
113122

@@ -130,9 +139,9 @@ When executed, this will show the following Dotted line plot:
130139

131140
## Line ticks
132141

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.
134143

135-
```
144+
```python
136145
import matplotlib.pyplot as plt
137146
import numpy as np
138147

@@ -166,9 +175,9 @@ When executed, this will show the following line ticks plot:
166175

167176
## Line with asymptote
168177

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.
170179

171-
```
180+
```python
172181
import matplotlib.pyplot as plt
173182
import numpy as np
174183

@@ -222,9 +231,9 @@ When executed, this will show the following Line with asymptote plot:
222231

223232
## Line with text scale
224233

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`.
226235

227-
```
236+
```python
228237
import matplotlib.pyplot as plt
229238
import numpy as np
230239

@@ -261,6 +270,7 @@ ax.spines['left'].set_position(('data', 0))
261270

262271
plt.show()
263272
```
273+
264274
When executed, this will show the following Line with text scale plot:
265275

266276
![Line with text scale](images/line-with-text-scale.png)

0 commit comments

Comments
 (0)