Scatter plot using Plotly in Python

Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization library.

Scatter Plot

A scatter plot is a diagram where each value is represented by the dot graph. Scatter  plot needs arrays for the same length, one for the value of x-axis and other value for the y-axis. Each data is represented as a dot point, whose location is given by x and y columns. It can be created using the scatter() method of plotly.express

Syntax: plotly.express.scatter(data_frame=None, x=None, y=None, color=None, symbol=None, size=None, hover_name=None, hover_data=None, custom_data=None, text=None, facet_row=None, facet_col=None, facet_col_wrap=0, error_x=None, error_x_minus=None, error_y=None, error_y_minus=None, animation_frame=None, animation_group=None, category_orders={}, labels={}, orientation=None, color_discrete_sequence=None, color_discrete_map={}, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, symbol_sequence=None, symbol_map={}, opacity=None, size_max=None, marginal_x=None, marginal_y=None, trendline=None, trendline_color_override=None, log_x=False, log_y=False, range_x=None, range_y=None, render_mode=’auto’, title=None, template=None, width=None, height=None)

Parameters:

Name Value Description
x, y array_like, shape (n, ) The data positions
s  scalar or array_like, shape (n, ), optional The marker size in points**2. Default is rcParams[‘lines.markersize’] ** 2.
c color, sequence, or sequence of color, optional

The marker color. Possible values:



A single color format string.

A sequence of color specifications of length n.

A sequence of n numbers to be mapped to colors using cmap and norm.

A 2-D array in which the rows are RGB or RGBA

marker  MarkerStyle, optional The marker style. marker can be either an instance of the class or the text shorthand for a particular marker. Defaults to None, in which case it takes the value of rcParams[“scatter.marker”] = ‘o’ = ‘o’. See markers for more information about marker styles.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.express as px
import numpy as np 
    
  
# creating random data through randomint 
# function of numpy.random 
np.random.seed(42
    
random_x= np.random.randint(1,101,100
random_y= np.random.randint(1,101,100
    
plot = px.scatter(random_x, random_y)
plot.show()

chevron_right


 Output:



Changing color

Color of dots of the scatter plot can be changed using the color argument of the scatter() method.

Example: We will use the built-in iris dataset.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.express as px
  
# Loading the iris dataset
df = px.data.iris()
  
fig = px.scatter(df, x="sepal_width", y="sepal_length",
                 color="species")
fig.show()

chevron_right


 Output:

Changing Size

The size of the dots of the scatter plot can be changed using the size argument of the scatter() method.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.express as px
  
# Loading the iris dataset
df = px.data.iris()
  
fig = px.scatter(df, x="sepal_width", y="sepal_length",
                 color="species", size='petal_length'
                 hover_data=['petal_width'])
fig.show()

chevron_right


Output:




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.