GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • PYTHON CHARTS
  • matplotlib
  • seaborn
  • plotly
  • pandas
  • colors
  • COLORS
  • COLOR PALETTES
  • R CODER
  • R CHARTS
  • Contact
  • Español

This view

  • Original page
  • New address

GeezerVison is showing python-charts.com — Scatter plot in plotly

Search for a graph

HOME CORRELATION SCATTER PLOT PLOTLY

Scatter plot in plotly

The scatter function from plotly

With the scatter function from Plotly Express it is very easy to create scatter plots with Plotly and Python. You will need to input your data as vector or as a pandas dataframe, as in the example below.

import plotly.express as px

fig = px.scatter(x = [0, 1, 2, 3, 4, 5],
                 y = [16, 8, 4, 2, 1, 0])


# Alternative: data from pandas data frame
# import pandas as pd
# df = pd.DataFrame({'x': [0, 1, 2, 3, 4, 5],
#                    'y': [16, 8, 4, 2, 1, 0]})

# Scatter plot from data frame
# fig = px.scatter(df, x = "x", y = "y")

fig.show()

Adding labels and a title

In case you want to add a title to your plot you can pass a string to title. If you want to customize the axis labels you will need to use the labels argument as follows.

import plotly.express as px

fig = px.scatter(x = [0, 1, 2, 3, 4, 5],
                 y = [16, 8, 4, 2, 1, 0],
                 title = "Title of the plot",
                 labels = {'x': 'X-axis', 'y':'Y-axis'})

fig.show()

Color customization

Color by group

You can input a categorical variable to the color argument in order to color the observations by group.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [0, 1, 2, 3, 4, 5],
                   'y': [16, 8, 4, 2, 1, 0],
                   'group': ['0', '1', '0', '1', '0', '1']})

# Scatter plot from data frame
fig = px.scatter(df, x = "x", y = "y", color = "group")
                 
fig.show()

Color based on a numerical variable

The color can also be assigned to a numerical variable. In this scenario, the observations will have a continuous color scale based on the values of the variable passed to color.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [0, 1, 2, 3, 4, 5],
                   'y': [16, 8, 4, 2, 1, 0]})

# Scatter plot from data frame
fig = px.scatter(df, x = "x", y = "y", color = "y")
                 
fig.show()

Symbols

Symbol by group

By default, all symbols are circles, but if you pass a categorical variable to symbol each group will have a different marker.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [0, 1, 2, 3, 4, 5],
                   'y': [16, 8, 4, 2, 1, 0],
                   'group': ['0', '1', '0', '1', '0', '1']})

# Scatter plot from data frame
fig = px.scatter(df, x = "x", y = "y", symbol = "group")
                 
fig.show()

Size of the symbols based on a variable

It is possible to set the size of the symbols or markers based on the values of a variable. In the following example we will use the y column to determine the size of the symbols, the greater the value, the greater the symbols.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [0, 1, 2, 3, 4, 5],
                   'y': [16, 8, 4, 2, 1, 0],
                   'group': ['0', '1', '0', '1', '0', '1']})

# Scatter plot from data frame
fig = px.scatter(df, x = "x", y = "y", color = "group", size = "y")
                 
fig.show()

Opacity

Note that you can control the opacity of the symbols with opacity. The possible values range between 0 and 1.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [0, 1, 2, 3, 4, 5],
                   'y': [16, 8, 4, 2, 1, 0]})

# Scatter plot from data frame
fig = px.scatter(df, x = "x", y = "y", opacity = 0.5)
                 
fig.show()

Scatter plot with marginal plots

The scatter function from Plotly provides the marginal_x and marginal_y arguments for adding marginal plots to the scatter plot. Possible options are "box" for box plots, "hist" for histograms, "violin" for violin plots and "rug" for rug plots.

Scatter plot with marginal box plots

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [0, 1, 2, 3, 4, 5],
                   'y': [16, 8, 4, 2, 1, 0]})

# Scatter plot with marginal boxplots
fig = px.scatter(df, x = "x", y = "y",
                 marginal_x = "box", marginal_y = "box") 
                 
fig.show()

Scatter plot with marginal histograms

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [0, 1, 2, 3, 4, 5],
                   'y': [16, 8, 4, 2, 1, 0]})

# Scatter plot with marginal rug plot
fig = px.scatter(df, x = "x", y = "y",
                 marginal_x = "histogram", marginal_y = "histogram") 
                 
fig.show()

Scatter plot with marginal violin plots

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [0, 1, 2, 3, 4, 5],
                   'y': [16, 8, 4, 2, 1, 0]})

# Scatter plot with marginal rug plot
fig = px.scatter(df, x = "x", y = "y",
                 marginal_x = "violin", marginal_y = "violin") 
                 
fig.show()

Scatter plot with marginal rug plots

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [0, 1, 2, 3, 4, 5],
                   'y': [16, 8, 4, 2, 1, 0]})

# Scatter plot with marginal rug plot
fig = px.scatter(df, x = "x", y = "y",
                 marginal_x = "rug", marginal_y = "rug") 
                 
fig.show()
R CODER website
Fundamentals of Data Visualization

Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon
Data Sketches

Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon

See also

Pairs plot (pairwise plot) in seaborn with the pairplot function

Pairs plot (pairwise plot) in seaborn with the pairplot function

Bubble chart in matplotlib

Bubble chart in matplotlib

Heatmaps in plotly with imshow

Heatmaps in plotly with imshow

Heatmap clustering in seaborn with clustermap

Heatmap clustering in seaborn with clustermap

2D histogram in matplotlib

2D histogram in matplotlib

Scatter plot in matplotlib

Scatter plot in matplotlib

Heat map in matplotlib

Heat map in matplotlib

Scatter plot with regression line in seaborn

Scatter plot with regression line in seaborn

Scatter plot in seaborn

Scatter plot in seaborn

Bubble chart in plotly

Bubble chart in plotly

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.