|
| 1 | +# Scatter Plots in Plotly |
| 2 | + |
| 3 | +* A scatter plot is a type of data visualization that uses dots to show values for two variables, with one variable on the x-axis and the other on the y-axis. It's useful for identifying relationships, trends, and correlations, as well as spotting clusters and outliers. |
| 4 | +* The dots on the plot shows how the variables are related. A scatter plot is made with the plotly library's `px.scatter()`. |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | + |
| 8 | +Before creating Scatter plots in Plotly you must ensure that you have Python, Plotly and Pandas installed on your system. |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +There are various ways to create Scatter plots 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. |
| 13 | + |
| 14 | +Here, we'll be using `plotly.express` to create the Scatter Plots. Also we'll be converting our datasets into pandas DataFrames which makes it extremely convenient and easy to create charts. |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +## Creating a simple Scatter Plot using `plotly.express.scatter` |
| 19 | + |
| 20 | +In `plotly.express.scatter`, each data point is represented as a marker point, whose location is given by the x and y columns. |
| 21 | + |
| 22 | +```Python |
| 23 | +import plotly.express as px |
| 24 | +import pandas as pd |
| 25 | + |
| 26 | +# Creating dataset |
| 27 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 28 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 29 | + |
| 30 | +# Converting dataset to pandas DataFrame |
| 31 | +dataset = {"Years": years, "Number of Cars sold": num_of_cars_sold} |
| 32 | +df = pd.DataFrame(dataset) |
| 33 | + |
| 34 | +# Creating scatter plot |
| 35 | +fig = px.scatter(df, x='Years', y='Number of Cars sold') |
| 36 | + |
| 37 | +# Showing plot |
| 38 | +fig.show() |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +Here, we are first creating the dataset and converting it into a pandas DataFrame using a dictionary, with its keys being DataFrame columns. Next, we are plotting the scatter plot by using `px.scatter`. In the `x` and `y` parameters, we have to specify a column name in the DataFrame. |
| 43 | + |
| 44 | +`px.scatter(df, x='Years', y='Number of Cars sold')` is used to specify that the scatter plot is to be plotted by taking the values from column `Years` for the x-axis and the values from column `Number of Cars sold` for the y-axis. |
| 45 | + |
| 46 | +Note: When you generate the image using the above code, it will show you an interactive plot. If you want an image, you can download it from the interactive plot itself. |
| 47 | + |
| 48 | +## Customizing Scatter Plots |
| 49 | + |
| 50 | +### Adding title to the plot |
| 51 | + |
| 52 | +Simply pass the title of your plot as a parameter in `px.scatter`. |
| 53 | + |
| 54 | +```Python |
| 55 | +import plotly.express as px |
| 56 | +import pandas as pd |
| 57 | + |
| 58 | +# Creating dataset |
| 59 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 60 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 61 | + |
| 62 | +# Converting dataset to pandas DataFrame |
| 63 | +dataset = {"Years": years, "Number of Cars sold": num_of_cars_sold} |
| 64 | +df = pd.DataFrame(dataset) |
| 65 | + |
| 66 | +# Creating scatter plot |
| 67 | +fig = px.scatter(df, x='Years', y='Number of Cars sold' ,title='Number of cars sold in various years') |
| 68 | + |
| 69 | +# Showing plot |
| 70 | +fig.show() |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | +### Adding bar colors and legends |
| 75 | + |
| 76 | +* To add different colors to different bars, simply pass the column name of the x-axis or a custom column which groups different bars in `color` parameter. |
| 77 | +* There are a lot of beautiful color scales available in plotly and can be found here [plotly color scales](https://plotly.com/python/builtin-colorscales/). Choose your favourite colorscale apply it like this: |
| 78 | + |
| 79 | +```Python |
| 80 | +import plotly.express as px |
| 81 | +import pandas as pd |
| 82 | + |
| 83 | +# Creating dataset |
| 84 | +flowers = ['Rose','Tulip','Marigold','Sunflower','Daffodil'] |
| 85 | +petals = [11,9,17,4,7] |
| 86 | + |
| 87 | +# Converting dataset to pandas DataFrame |
| 88 | +dataset = {'flowers':flowers, 'petals':petals} |
| 89 | +df = pd.DataFrame(dataset) |
| 90 | + |
| 91 | +# Creating pie chart |
| 92 | +fig = px.pie(df, values='petals', names='flowers', |
| 93 | + title='Number of Petals in Flowers', |
| 94 | + color_discrete_sequence=px.colors.sequential.Agsunset) |
| 95 | + |
| 96 | +# Showing plot |
| 97 | +fig.show() |
| 98 | +``` |
| 99 | + |
| 100 | + |
| 101 | +You can also set custom colors for each label by passing it as a dictionary(map) in `color_discrete_map`, like this: |
| 102 | + |
| 103 | +```Python |
| 104 | +import plotly.express as px |
| 105 | +import pandas as pd |
| 106 | + |
| 107 | +# Creating dataset |
| 108 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 109 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 110 | + |
| 111 | +# Converting dataset to pandas DataFrame |
| 112 | +dataset = {"Years": years, "Number of Cars sold": num_of_cars_sold} |
| 113 | +df = pd.DataFrame(dataset) |
| 114 | + |
| 115 | +# Creating scatter plot |
| 116 | +fig = px.scatter(df, x='Years', |
| 117 | + y='Number of Cars sold' , |
| 118 | + title='Number of cars sold in various years', |
| 119 | + color='Years', |
| 120 | + color_discrete_map={'1998':'red', |
| 121 | + '1999':'magenta', |
| 122 | + '2000':'green', |
| 123 | + '2001':'yellow', |
| 124 | + '2002':'royalblue'}) |
| 125 | + |
| 126 | +# Showing plot |
| 127 | +fig.show() |
| 128 | +``` |
| 129 | + |
| 130 | + |
| 131 | +### Setting Size of Scatter |
| 132 | + |
| 133 | +We may want to set the size of different scatters for visibility differences between categories. This can be done by using the `size` parameter in `px.scatter`, where we specify a column in the DataFrame that determines the size of each scatter point. |
| 134 | + |
| 135 | +```Python |
| 136 | +import plotly.express as px |
| 137 | +import pandas as pd |
| 138 | + |
| 139 | +# Creating dataset |
| 140 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 141 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 142 | + |
| 143 | +# Converting dataset to pandas DataFrame |
| 144 | +dataset = {"Years": years, "Number of Cars sold": num_of_cars_sold} |
| 145 | +df = pd.DataFrame(dataset) |
| 146 | + |
| 147 | +# Creating scatter plot |
| 148 | +fig = px.scatter(df, x='Years', |
| 149 | + y='Number of Cars sold' , |
| 150 | + title='Number of cars sold in various years', |
| 151 | + color='Years', |
| 152 | + color_discrete_map={'1998':'red', |
| 153 | + '1999':'magenta', |
| 154 | + '2000':'green', |
| 155 | + '2001':'yellow', |
| 156 | + '2002':'royalblue'}, |
| 157 | + size='Number of Cars sold') |
| 158 | + |
| 159 | +# Showing plot |
| 160 | +fig.show() |
| 161 | +``` |
| 162 | + |
| 163 | + |
| 164 | +### Giving a hover effect |
| 165 | + |
| 166 | +you can use the `hover_name` and `hover_data` parameters in `px.scatter`. The `hover_name` parameter specifies the column to use for the `hover text`, and the `hover_data` parameter allows you to specify additional data to display when hovering over a point |
| 167 | + |
| 168 | +```Python |
| 169 | +import plotly.express as px |
| 170 | +import pandas as pd |
| 171 | + |
| 172 | +# Creating dataset |
| 173 | +years = ['1998', '1999', '2000', '2001', '2002'] |
| 174 | +num_of_cars_sold = [200, 300, 500, 700, 1000] |
| 175 | + |
| 176 | +# Converting dataset to pandas DataFrame |
| 177 | +dataset = {"Years": years, "Number of Cars sold": num_of_cars_sold} |
| 178 | +df = pd.DataFrame(dataset) |
| 179 | + |
| 180 | +# Creating scatter plot |
| 181 | +fig = px.scatter(df, x='Years', |
| 182 | + y='Number of Cars sold' , |
| 183 | + title='Number of cars sold in various years', |
| 184 | + color='Years', |
| 185 | + color_discrete_map={'1998':'red', |
| 186 | + '1999':'magenta', |
| 187 | + '2000':'green', |
| 188 | + '2001':'yellow', |
| 189 | + '2002':'royalblue'}, |
| 190 | + size='Number of Cars sold', |
| 191 | + hover_name='Years', |
| 192 | + hover_data={'Number of Cars sold': True}) |
| 193 | + |
| 194 | +# Showing plot |
| 195 | +fig.show() |
| 196 | +``` |
| 197 | + |
| 198 | + |
0 commit comments