|
| 1 | +# Line Charts in Plotly |
| 2 | + |
| 3 | +A line chart displays information as a series of data points connected by straight line segments. It represents the change in a quantity with respect to another quantity and helps us to see trends and patterns over time or across categories. It is a basic type of chart common in many fields. For example, it is used to represent the price of stocks with respect to time, among many others. |
| 4 | + |
| 5 | +It is one of the most widely used type of data visualisation as it is easy to interpret and is pleasing to the eyes. |
| 6 | + |
| 7 | +Plotly is a very powerful library for creating modern visualizations and it provides a very easy and intuitive method to create highly customized line charts. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +Before creating line charts in Plotly you must ensure that you have Python, Plotly and Pandas installed on your system. |
| 12 | + |
| 13 | +## Introduction |
| 14 | + |
| 15 | +There are various ways to create line charts in `plotly`. One of the prominent and easiest one is using `plotly.express`. Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures. On the other hand you can also use `plotly.graph_objects` to create various plots. |
| 16 | + |
| 17 | +Here, we'll be using `plotly.express` to create the line charts. Also we'll be converting our datasets into pandas DataFrames which makes it extremely convenient to create plots. |
| 18 | + |
| 19 | +Also, note that when you execute the codes in a simple python file, the output plot will be shown in your **browser**, rather than a pop-up window like in matplotlib. If you do not want that, it is **recommended to create the plots in a notebook (like jupyter)**. For this, install an additional library `nbformat`. This way you can see the output on the notebook itself, and can also render its format to png, jpg, etc. |
| 20 | + |
| 21 | +## Creating a simple line chart using `plotly.express.line` |
| 22 | + |
| 23 | +With `plotly.express.line`, each data point is represented as a vertex (which location is given by the x and y columns) of a polyline mark in 2D space. |
| 24 | + |
| 25 | +```Python |
| 26 | +import plotly.express as px |
| 27 | +import pandas as pd |
| 28 | + |
| 29 | +# Creating dataset |
| 30 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 31 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 32 | + |
| 33 | +# Converting dataset to pandas DataFrame |
| 34 | +dataset = {"Years":years, "Number of Cars sold":num_of_cars_sold} |
| 35 | +df = pd.DataFrame(dataset) |
| 36 | + |
| 37 | +# Creating line chart |
| 38 | +fig = px.line(df, x='Years', y='Number of Cars sold') |
| 39 | + |
| 40 | +# Showing plot |
| 41 | +fig.show() |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +Here, we are first creating the dataset and converting it into Pandas DataFrames using dictionaries, with its keys being DataFrame columns. Next, we are plotting the line chart by using `px.line`. In the `x` and `y` parameters, we have to specify a column name in the DataFrame. |
| 47 | + |
| 48 | +**Note:** When you generate the image using above code, it will show you an **interactive plot**, if you want image, you can download it from their itself. |
| 49 | + |
| 50 | +## Customizing Line Charts |
| 51 | + |
| 52 | +### Adding title to the chart |
| 53 | + |
| 54 | +Simply pass the title of your graph as a parameter in `px.line`. |
| 55 | + |
| 56 | +```Python |
| 57 | +import plotly.express as px |
| 58 | +import pandas as pd |
| 59 | + |
| 60 | +# Creating dataset |
| 61 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 62 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 63 | + |
| 64 | +# Converting dataset to pandas DataFrame |
| 65 | +dataset = {"Years":years, "Number of Cars sold":num_of_cars_sold} |
| 66 | +df = pd.DataFrame(dataset) |
| 67 | + |
| 68 | +# Creating line chart |
| 69 | +fig = px.line(df, x='Years', y='Number of Cars sold', |
| 70 | + title='Number of cars sold in various years') |
| 71 | + |
| 72 | +# Showing plot |
| 73 | +fig.show() |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +### Adding Markers to the lines |
| 79 | + |
| 80 | +The `markers` argument can be set to `True` to show markers on lines. |
| 81 | + |
| 82 | +```Python |
| 83 | +import plotly.express as px |
| 84 | +import pandas as pd |
| 85 | + |
| 86 | +# Creating dataset |
| 87 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 88 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 89 | + |
| 90 | +# Converting dataset to pandas DataFrame |
| 91 | +dataset = {"Years":years, "Number of Cars sold":num_of_cars_sold} |
| 92 | +df = pd.DataFrame(dataset) |
| 93 | + |
| 94 | +# Creating line chart |
| 95 | +fig = px.line(df, x='Years', y='Number of Cars sold', |
| 96 | + title='Number of cars sold in various years', |
| 97 | + markers=True) |
| 98 | + |
| 99 | +# Showing plot |
| 100 | +fig.show() |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +### Dashed Lines |
| 106 | + |
| 107 | +You can plot dashed lines by changing the `dash` property of `line` to `dash` or `longdash` and passing it as a dictionary to `patch` parameter in `fig.update_traces`. |
| 108 | + |
| 109 | +```Python |
| 110 | +import plotly.express as px |
| 111 | +import pandas as pd |
| 112 | + |
| 113 | +# Creating dataset |
| 114 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 115 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 116 | + |
| 117 | +# Converting dataset to pandas DataFrame |
| 118 | +dataset = {"Years":years, "Number of Cars sold":num_of_cars_sold} |
| 119 | +df = pd.DataFrame(dataset) |
| 120 | + |
| 121 | +# Creating line chart |
| 122 | +fig = px.line(df, x='Years', y='Number of Cars sold', |
| 123 | + title='Number of cars sold in various years') |
| 124 | + |
| 125 | +fig.update_traces(patch={"line": {"dash": 'dash'}}) |
| 126 | + |
| 127 | +# Showing plot |
| 128 | +fig.show() |
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +### Dotted Lines |
| 134 | + |
| 135 | +You can plot dotted lines by changing the `dash` property of `line` to `dot` and passing it as a dictionary to `patch` parameter in `fig.update_traces`. |
| 136 | + |
| 137 | +```Python |
| 138 | +import plotly.express as px |
| 139 | +import pandas as pd |
| 140 | + |
| 141 | +# Creating dataset |
| 142 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 143 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 144 | + |
| 145 | +# Converting dataset to pandas DataFrame |
| 146 | +dataset = {"Years":years, "Number of Cars sold":num_of_cars_sold} |
| 147 | +df = pd.DataFrame(dataset) |
| 148 | + |
| 149 | +# Creating line chart |
| 150 | +fig = px.line(df, x='Years', y='Number of Cars sold', |
| 151 | + title='Number of cars sold in various years') |
| 152 | + |
| 153 | +fig.update_traces(patch={"line": {"dash": 'dot'}}) |
| 154 | + |
| 155 | +# Showing plot |
| 156 | +fig.show() |
| 157 | +``` |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | +### Dashed and Dotted Lines |
| 162 | + |
| 163 | +You can plot dotted lines by changing the `dash` property of `line` to `dashdot` and passing it as a dictionary to `patch` parameter in `fig.update_traces`. |
| 164 | + |
| 165 | +```Python |
| 166 | +import plotly.express as px |
| 167 | +import pandas as pd |
| 168 | + |
| 169 | +# Creating dataset |
| 170 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 171 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 172 | + |
| 173 | +# Converting dataset to pandas DataFrame |
| 174 | +dataset = {"Years":years, "Number of Cars sold":num_of_cars_sold} |
| 175 | +df = pd.DataFrame(dataset) |
| 176 | + |
| 177 | +# Creating line chart |
| 178 | +fig = px.line(df, x='Years', y='Number of Cars sold', |
| 179 | + title='Number of cars sold in various years') |
| 180 | + |
| 181 | +fig.update_traces(patch={"line": {"dash": 'dashdot'}}) |
| 182 | + |
| 183 | +# Showing plot |
| 184 | +fig.show() |
| 185 | +``` |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +### Changing line colors |
| 190 | + |
| 191 | +You can set custom colors to lines by changing the `color` property of `line` to `your_color` and passing it as a dictionary to `patch` parameter in `fig.update_traces`. |
| 192 | + |
| 193 | +```Python |
| 194 | +import plotly.express as px |
| 195 | +import pandas as pd |
| 196 | + |
| 197 | +# Creating dataset |
| 198 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 199 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 200 | + |
| 201 | +# Converting dataset to pandas DataFrame |
| 202 | +dataset = {"Years":years, "Number of Cars sold":num_of_cars_sold} |
| 203 | +df = pd.DataFrame(dataset) |
| 204 | + |
| 205 | +# Creating line chart |
| 206 | +fig = px.line(df, x='Years', y='Number of Cars sold', |
| 207 | + title='Number of cars sold in various years') |
| 208 | + |
| 209 | +fig.update_traces(patch={"line": {"color": 'red'}}) |
| 210 | + |
| 211 | +# Showing plot |
| 212 | +fig.show() |
| 213 | +``` |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | +### Changing line width |
| 218 | + |
| 219 | +You can set custom width to lines by changing the `width` property of `line` to `your_width` and passing it as a dictionary to `patch` parameter in `fig.update_traces`. |
| 220 | + |
| 221 | +```Python |
| 222 | +import plotly.express as px |
| 223 | +import pandas as pd |
| 224 | + |
| 225 | +# Creating dataset |
| 226 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 227 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 228 | + |
| 229 | +# Converting dataset to pandas DataFrame |
| 230 | +dataset = {"Years":years, "Number of Cars sold":num_of_cars_sold} |
| 231 | +df = pd.DataFrame(dataset) |
| 232 | + |
| 233 | +# Creating line chart |
| 234 | +fig = px.line(df, x='Years', y='Number of Cars sold', |
| 235 | + title='Number of cars sold in various years') |
| 236 | + |
| 237 | +fig.update_traces(patch={"line": {"width": 7}}) |
| 238 | + |
| 239 | +# Showing plot |
| 240 | +fig.show() |
| 241 | +``` |
| 242 | + |
| 243 | + |
| 244 | + |
| 245 | +### Labeling Data Points |
| 246 | + |
| 247 | +You can label your data points by passing the relevant column name of your DataFrame to `text` parameter in `px.line`. |
| 248 | + |
| 249 | +```Python |
| 250 | +# Creating dataset |
| 251 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 252 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 253 | + |
| 254 | +# Converting dataset to pandas DataFrame |
| 255 | +dataset = {"Years":years, "Number of Cars sold":num_of_cars_sold} |
| 256 | +df = pd.DataFrame(dataset) |
| 257 | + |
| 258 | +# Creating line chart |
| 259 | +fig = px.line(df, x='Years', y='Number of Cars sold', |
| 260 | + title='Number of cars sold in various years', |
| 261 | + text='Number of Cars sold') |
| 262 | + |
| 263 | +fig.update_traces(textposition="bottom right") |
| 264 | + |
| 265 | +# Showing plot |
| 266 | +fig.show() |
| 267 | +``` |
| 268 | + |
| 269 | + |
| 270 | + |
| 271 | +## Plotting multiple lines |
| 272 | + |
| 273 | +There are several ways to plot multiple lines in plotly, like using `plotly.graph_objects`, using `fig.add_scatter`, having multiple columns in the DataFrame, etc. |
| 274 | + |
| 275 | +Here, we'll be creating a simple dataset of the runs scored by the end of each over by India and South Africa in recent T20 World Cup Final and plot it using plotly. |
| 276 | + |
| 277 | +```Python |
| 278 | +import plotly.express as px |
| 279 | +import pandas as pd |
| 280 | + |
| 281 | +# Creating dataset |
| 282 | +overs = list(range(0,21)) |
| 283 | +runs_india = [0,15,23,26,32,39,45,49,59,68,75,82,93,98,108,118,126,134,150,167,176] |
| 284 | +runs_rsa = [0,6,11,14,22,32,42,49,62,71,81,93,101,109,123,147,151,155,157,161,169] |
| 285 | + |
| 286 | +# Converting dataset to pandas DataFrame |
| 287 | +dataset = {"overs":overs, "India":runs_india, "South Africa":runs_rsa} |
| 288 | +df = pd.DataFrame(dataset) |
| 289 | + |
| 290 | +# Creating line chart |
| 291 | +fig = px.line(df, x="overs", y=["India", "South Africa"]) |
| 292 | +fig.update_layout(xaxis_title="Overs", yaxis_title="Runs", legend_title=None) |
| 293 | + |
| 294 | +# Showing plot |
| 295 | +fig.show() |
| 296 | +``` |
| 297 | + |
| 298 | + |
| 299 | + |
| 300 | +To plot multiple lines, we have passed multiple columns of the DataFrame in the `y` parameter. |
0 commit comments