|
| 1 | +# Line Chart in Matplotlib |
| 2 | + |
| 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 | + |
| 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. |
| 9 | + |
| 10 | +## Prerequisites |
| 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 |
| 16 | +``` |
| 17 | + |
| 18 | +## Creating a simple Line Plot |
| 19 | + |
| 20 | +First import matplotlib and numpy, these are useful for charting. |
| 21 | + |
| 22 | +You can use the `plot(x,y)` method to create a line chart. |
| 23 | + |
| 24 | +```python |
| 25 | +import matplotlib.pyplot as plt |
| 26 | +import numpy as np |
| 27 | + |
| 28 | +x = np.linspace(-1, 1, 50) |
| 29 | +print(x) |
| 30 | +y = 2*x + 1 |
| 31 | + |
| 32 | +plt.plot(x, y) |
| 33 | +plt.show() |
| 34 | +``` |
| 35 | + |
| 36 | +When executed, this will show the following line plot: |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +## Curved line |
| 42 | + |
| 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 |
| 46 | +import matplotlib.pyplot as plt |
| 47 | +import numpy as np |
| 48 | + |
| 49 | +x = np.linspace(-1, 1, 50) |
| 50 | +y = 2**x + 1 |
| 51 | + |
| 52 | +plt.plot(x, y) |
| 53 | +plt.show() |
| 54 | +``` |
| 55 | + |
| 56 | +When executed, this will show the following Curved line plot: |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +## Line with Labels |
| 62 | + |
| 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 |
| 66 | +import matplotlib.pyplot as plt |
| 67 | +import numpy as np |
| 68 | + |
| 69 | +x = np.linspace(-1, 1, 50) |
| 70 | +y1 = 2*x + 1 |
| 71 | +y2 = 2**x + 1 |
| 72 | + |
| 73 | +plt.figure() |
| 74 | +plt.plot(x, y1) |
| 75 | + |
| 76 | +plt.xlabel("I am x") |
| 77 | +plt.ylabel("I am y") |
| 78 | +plt.title("With Labels") |
| 79 | + |
| 80 | +plt.show() |
| 81 | +``` |
| 82 | + |
| 83 | +When executed, this will show the following line with labels plot: |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +## Multiple lines |
| 88 | + |
| 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. |
| 90 | + |
| 91 | +```python |
| 92 | +import matplotlib.pyplot as plt |
| 93 | +import numpy as np |
| 94 | + |
| 95 | +x = np.linspace(-1, 1, 50) |
| 96 | +y1 = 2*x + 1 |
| 97 | +y2 = 2**x + 1 |
| 98 | + |
| 99 | +plt.figure(num = 3, figsize=(8, 5)) |
| 100 | +plt.plot(x, y2) |
| 101 | +plt.plot(x, y1, |
| 102 | + color='red', |
| 103 | + linewidth=1.0, |
| 104 | + linestyle='--' |
| 105 | + ) |
| 106 | + |
| 107 | +plt.show() |
| 108 | +``` |
| 109 | + |
| 110 | +When executed, this will show the following Multiple lines plot: |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +## Dotted line |
| 116 | + |
| 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. |
| 118 | + |
| 119 | +```python |
| 120 | +import matplotlib.pyplot as plt |
| 121 | +import numpy as np |
| 122 | + |
| 123 | +n = 1024 |
| 124 | +X = np.random.normal(0, 1, n) |
| 125 | +Y = np.random.normal(0, 1, n) |
| 126 | +T = np.arctan2(X, Y) |
| 127 | + |
| 128 | +plt.scatter(np.arange(5), np.arange(5)) |
| 129 | + |
| 130 | +plt.xticks(()) |
| 131 | +plt.yticks(()) |
| 132 | + |
| 133 | +plt.show() |
| 134 | +``` |
| 135 | + |
| 136 | +When executed, this will show the following Dotted line plot: |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +## Line ticks |
| 141 | + |
| 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. |
| 143 | + |
| 144 | +```python |
| 145 | +import matplotlib.pyplot as plt |
| 146 | +import numpy as np |
| 147 | + |
| 148 | +x = np.linspace(-1, 1, 50) |
| 149 | +y = 2*x - 1 |
| 150 | + |
| 151 | +plt.figure(figsize=(12, 8)) |
| 152 | +plt.plot(x, y, color='r', linewidth=10.0, alpha=0.5) |
| 153 | + |
| 154 | +ax = plt.gca() |
| 155 | + |
| 156 | +ax.spines['right'].set_color('none') |
| 157 | +ax.spines['top'].set_color('none') |
| 158 | + |
| 159 | +ax.xaxis.set_ticks_position('bottom') |
| 160 | +ax.yaxis.set_ticks_position('left') |
| 161 | + |
| 162 | +ax.spines['bottom'].set_position(('data', 0)) |
| 163 | +ax.spines['left'].set_position(('data', 0)) |
| 164 | + |
| 165 | +for label in ax.get_xticklabels() + ax.get_yticklabels(): |
| 166 | + label.set_fontsize(12) |
| 167 | + label.set_bbox(dict(facecolor='y', edgecolor='None', alpha=0.7)) |
| 168 | + |
| 169 | +plt.show() |
| 170 | +``` |
| 171 | + |
| 172 | +When executed, this will show the following line ticks plot: |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | +## Line with asymptote |
| 177 | + |
| 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. |
| 179 | + |
| 180 | +```python |
| 181 | +import matplotlib.pyplot as plt |
| 182 | +import numpy as np |
| 183 | + |
| 184 | +x = np.linspace(-1, 1, 50) |
| 185 | +y1 = 2*x + 1 |
| 186 | +y2 = 2**x + 1 |
| 187 | + |
| 188 | +plt.figure(figsize=(12, 8)) |
| 189 | +plt.plot(x, y2) |
| 190 | +plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--') |
| 191 | + |
| 192 | +ax = plt.gca() |
| 193 | + |
| 194 | +ax.spines['right'].set_color('none') |
| 195 | +ax.spines['top'].set_color('none') |
| 196 | + |
| 197 | +ax.xaxis.set_ticks_position('bottom') |
| 198 | +ax.yaxis.set_ticks_position('left') |
| 199 | + |
| 200 | +ax.spines['bottom'].set_position(('data', 0)) |
| 201 | +ax.spines['left'].set_position(('data', 0)) |
| 202 | + |
| 203 | + |
| 204 | +x0 = 1 |
| 205 | +y0 = 2*x0 + 1 |
| 206 | + |
| 207 | +plt.scatter(x0, y0, s = 66, color = 'b') |
| 208 | +plt.plot([x0, x0], [y0, 0], 'k-.', lw= 2.5) |
| 209 | + |
| 210 | +plt.annotate(r'$2x+1=%s$' % |
| 211 | + y0, |
| 212 | + xy=(x0, y0), |
| 213 | + xycoords='data', |
| 214 | + |
| 215 | + xytext=(+30, -30), |
| 216 | + textcoords='offset points', |
| 217 | + fontsize=16, |
| 218 | + arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2') |
| 219 | + ) |
| 220 | + |
| 221 | +plt.text(0, 3, |
| 222 | + r'$This\ is\ a\ good\ idea.\ \mu\ \sigma_i\ \alpha_t$', |
| 223 | + fontdict={'size':16,'color':'r'}) |
| 224 | + |
| 225 | +plt.show() |
| 226 | +``` |
| 227 | + |
| 228 | +When executed, this will show the following Line with asymptote plot: |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | +## Line with text scale |
| 233 | + |
| 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`. |
| 235 | + |
| 236 | +```python |
| 237 | +import matplotlib.pyplot as plt |
| 238 | +import numpy as np |
| 239 | + |
| 240 | +x = np.linspace(-1, 1, 50) |
| 241 | +y1 = 2*x + 1 |
| 242 | +y2 = 2**x + 1 |
| 243 | + |
| 244 | +plt.figure(num = 3, figsize=(8, 5)) |
| 245 | +plt.plot(x, y2) |
| 246 | + |
| 247 | +plt.plot(x, y1, |
| 248 | + color='red', |
| 249 | + linewidth=1.0, |
| 250 | + linestyle='--' |
| 251 | + ) |
| 252 | + |
| 253 | +plt.xlim((-1, 2)) |
| 254 | +plt.ylim((1, 3)) |
| 255 | + |
| 256 | +new_ticks = np.linspace(-1, 2, 5) |
| 257 | +plt.xticks(new_ticks) |
| 258 | +plt.yticks([-2, -1.8, -1, 1.22, 3], |
| 259 | + [r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$readly\ good$']) |
| 260 | + |
| 261 | +ax = plt.gca() |
| 262 | +ax.spines['right'].set_color('none') |
| 263 | +ax.spines['top'].set_color('none') |
| 264 | + |
| 265 | +ax.xaxis.set_ticks_position('bottom') |
| 266 | +ax.yaxis.set_ticks_position('left') |
| 267 | + |
| 268 | +ax.spines['bottom'].set_position(('data', 0)) |
| 269 | +ax.spines['left'].set_position(('data', 0)) |
| 270 | + |
| 271 | +plt.show() |
| 272 | +``` |
| 273 | + |
| 274 | +When executed, this will show the following Line with text scale plot: |
| 275 | + |
| 276 | + |
| 277 | + |
| 278 | + |
0 commit comments