In this simple tutorial, we’ll see how to create a remarkable scatterplot in Python using one of the best APIs for data science and visualization, of course, I’m talking of Plotly a fantastic API for charts.
Scatterplot
As you’re probably aware, scatterplots allow the comparison of two numeric variables for a set of data. Depending on the trend of the scatter points, we could interpret a correlation between those variables. Let’s jump to code!
Scatterplot in Plotly
In Plotly is easy to create wonderful customized charts, let’s see scatterplots in a few lines of codes
import pandas as pd import plotly.offline as py import plotly.graph_objects as go import numpy as np np.random.seed(42) # Create our x and y data random_x = np.random.randint(1, 101, 100) random_y = np.random.randint(1, 101, 100) # In plotly the charts are named traces trace = go.Scatter(x = random_x, y = random_y, mode='markers') layout = go.Layout(title='My Simple Scatterplot', xaxis = {'title': 'Random X'}, yaxis = {'title': 'Random Y'}) fig = go.Figure(data = trace, layout = layout) py.plot(fig, filename='scatter-demo')
The previous one is the simplest scatterplot that you can make with Plotly through the module graph_objects. In particular, the class Scatter is employed to reach our objective passing the following, basic, parameters:
- X: is a list of numeric values to show along the x-axis
- Y: is a list of numeric values to show along the y-axis
- Mode: define the modality of the draw, other possible values are ‘lines’, ‘line+markers’ and so on (see my tutorial on how to make a line chart)
You can refer to the documentation to discover the several parameters that the Scatter class has.
Sophisticated Scatter Plot
Now, let’s try to customize our chart playing on those parameters.
import pandas as pd import numpy as np import plotly.graph_objects as go import plotly.offline as py np.random.seed(42) # Create our x and y data random_x = np.random.randint(1, 101, 100) random_y = np.random.randint(1, 101, 100) # In plotly the charts are named traces trace = go.Scatter(x = random_x, y = random_y, mode='markers', selectedpoints=[0, 10, 50, 90], # Points' indexes you want highlight selected=dict( # Define a style for the selected points marker=dict( color = 'rgb(255, 60, 60)', # color size = 24 # in px )), marker=dict( # Define the style of overall points in the chart color = 'rgb(51, 204, 153)', # color size = 12, # in px symbol = 'hexagon' # customized marker symbol )) layout = go.Layout(title='My Sophisticated Scatterplot', xaxis = {'title': 'Random X'}, yaxis = {'title': 'Random Y'}) fig = go.Figure(data = trace, layout = layout) py.plot(fig, filename='scatter-demo')
It is possible to notice that this chart is a little bit more sophisticated than the first one. Indeed, we added more information to the chart through the parameters of the class Scatter. Just a few comments on the code.
Passing the data
First, as in the previous chart, we passed our data and set markers as the mode
x = random_x, y = random_y, mode='markers'
Selecting the Points
Second, we select some random points of the distribution passing a list of points index to the parameter selected points. Below are details from the documentation:
- SelectedPoints: Array containing integer indices of selected points. Has an effect only for traces that support selections
selectedpoints=[0, 10, 50, 90]
Define selected markers’ style
Third, we define the style of the selected points changing the colour (as red) and the size. Below is the parameter description:
- Selected: Instance or dict with compatible properties. One of the properties is marker that define the style of the selected points (refer to documentation)
# Define a style for the selected points selected=dict(marker=dict(color = 'rgb(255, 60, 60)', # color size = 24))
The general style
Finally, we define a style for the overall points in the chart choosing the symbol (as hexagon), the size (as 12px) and the colour (as green), in particular for the colours we used the RGB scale
# Define the style of overall points in the chart marker=dict(color = 'rgb(51, 204, 153)', # color size = 12, # in px symbol = 'hexagon') # customized marker symbol
Note: you can define markers style passing the class Marker or a dictionary-like data structure. All the possible configurations can be discovered in the documentation.
Conclusion
To conclude, Plotly is an easy-to-use library to create wonderful and interactive visualization and consequently beautiful dashboards.
See you in the next article!
You might also like
More from Data Visualisation
Plotly – Bar Chart Beautiful Ways Show Category data in Python
Quick tutorial starting from the most simple bar chart to create high-quality bar chart using Python and Plotly
Plotly – Wonderful Line chart in Python
Brief Python tutorial on line chart in Plotly