Scatter plot in Plotly using graph_objects class

30 Aug, 2021
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 are those charts in which data points are represented horizontally and on vertical axis to show that how one variable affect on another variable. The scatter() method of graph_objects class produces a scatter trace. The mode of the property decides the appearance of data points.
Syntax: plotly.graph_objects.Scatter(arg=None, cliponaxis=None, connectgaps=None, customdata=None, customdatasrc=None, dx=None, dy=None, error_x=None, error_y=None, fill=None, fillcolor=None, groupnorm=None, hoverinfo=None, hoverinfosrc=None, hoverlabel=None, hoveron=None, hovertemplate=None, hovertemplatesrc=None, hovertext=None, hovertextsrc=None, ids=None, idssrc=None, legendgroup=None, line=None, marker=None, meta=None, metasrc=None, mode=None, name=None, opacity=None, orientation=None, r=None, rsrc=None, selected=None, selectedpoints=None, showlegend=None, stackgaps=None, stackgroup=None, stream=None, t=None, text=None, textfont=None, textposition=None, textpositionsrc=None, textsrc=None, texttemplate=None, texttemplatesrc=None, tsrc=None, uid=None, uirevision=None, unselected=None, visible=None, x=None, x0=None, xaxis=None, xcalendar=None, xsrc=None, y=None, y0=None, yaxis=None, ycalendar=None, ysrc=None, **kwargs)
Parameters:
Example:
Python3
import plotly.graph_objects as pximport numpy as np  # creating random data through randomint# function of numpy.randomnp.random.seed(42) random_x= np.random.randint(1,101,100)random_y= np.random.randint(1,101,100) plot = px.Figure(data=[px.Scatter(    x = random_x,    y = random_y,    mode = 'markers',)])                  plot.show()
 
 
Output:
 
 
Color scale can be shown using the showscale parameter. This parameter takes a boolean value. If the value is true then the scale is shown otherwise not.
 
Example:
 
Python3
import plotly.graph_objects as goimport numpy as np  n = 10000plot = go.Figure(data=[go.Scatter(    x = np.random.randn(n),    mode = 'markers',    marker=dict(        color=np.random.randn(n),        colorscale='Viridis',        showscale=True    ))])                  plot.show()
 
 
Output:
 
 
In scatter plot can be styled using keywords arguments, let’s see the examples given below:
 
Example 1: Changing the color of the graph
 
Python3
import plotly.graph_objects as pximport numpy as np  # creating random data through randomint# function of numpy.randomnp.random.seed(42) random_x= np.random.randint(1,101,100)random_y= np.random.randint(1,101,100) plot = px.Figure(data=[px.Scatter(    x = random_x,    y = random_y,    mode = 'markers',    marker_color='rgba(199, 10, 165, .9)')])                  plot.show()
 
 
Output:
 
 
Example 2: Using tips dataset
 
Python3
import plotly.graph_objects as pximport plotly.express as goimport numpy as np df = go.data.tips() x = df['total_bill']y = df['day'] plot = px.Figure(data=[px.Scatter(    x = x,    y = y,    mode = 'markers',    marker_color='rgba(199, 10, 165, .9)')])            plot.update_traces(mode='markers', marker_size=10)       plot.show()
 
 
Output:
 
 
The bubble scatter plot can be created using the marker size. Marker size and color are used to control the overall size of the marker. Marker size helps to maintain the color inside the bubble in the graph.
 
Example:
 
Python3
import plotly.graph_objects as pximport numpy as np   # creating random data through randomint# function of numpy.randomnp.random.seed(42)  random_x= np.random.randint(1,101,100)random_y= np.random.randint(1,101,100)  plot = px.Figure(data=[px.Scatter(    x = random_x,    y = random_y,    mode = 'markers',    marker_size = [115, 20, 30])])                   plot.show()
 
 
Output:
 
 
adnanirshad158
Python-Plotly
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Convert integer to string in Python