.scatter()
The .scatter()
method in the Plotly library creates a scatter plot to visualize the relationship between variables using markers on a Cartesian plane, with data points plotted based on their values on the x
and y
axes.
Syntax
plotly.express.scatter(data_frame=None, x=None, y=None, color=None, symbol=None, size=None, ...)
data_frame
: The PandasDataFrame
holding the data to visualize.x
: The column name indata_frame
,Series
or array_like object for x-axis data.y
: The column name indata_frame
,Series
or array_like object for y-axis data.color
: The column name indata_frame
,Series
or array_like object specifying marker colors.symbol
: The column name indata_frame
,Series
or array_like object assigning marker symbols.size
: The column name indata_frame
,Series
or array_like object assgining marker sizes.
Both the x
and y
parameters are required and represent either a string, integer, Series
, or array-like object. Other parameters are optional and can modify plot features such as marker sizes and/or colors. If data_frame
is missing, a DataFrame
is constructed using the other arguments.
Note: The ellipsis in the syntax (…) indicates that there can be additional optional parameters beyond those listed here to customize the scatter plot.
Example
The below example demonstrates the use of the .scatter()
method:
# Defining 'x' and 'y' as array_like objectsimport plotly.express as pxx = [1, 3, 5, 7, 9]y = [4, 6, 5, 8, 2]# Creating a scatter plotfig = px.scatter(x = x, y = y)# Displaying the plotfig.show()
The output for the above code is as follows:
Here is another example that customizes the previously created scatter plot by adding colors and symbols for each data point:
import plotly.express as px# Adding 'color' and 'symbol' columns to the already existing 'x' and 'y' columnsx = [1, 3, 5, 7, 9]y = [4, 6, 5, 8, 2]color = ['red', 'green', 'blue', 'purple', 'orange']symbol = ['circle', 'square', 'diamond', 'star', 'triangle-up']# Creating a scatter plotfig = px.scatter(x=x, y=y, color=color, symbol=symbol, title = "Scatter Plot with Colors and Symbols")# Displaying the plotfig.show()
The above code produces the following output:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.