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