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 pandas

Search for a graph

HOME CORRELATION SCATTER PLOT PANDAS

Scatter plot in pandas

Scatter plots show the relationship between two numeric variables. In pandas they are created with plot.scatter, indicating the columns for the X and Y axes.

Scatter plot with plot.scatter

Pass the names of the columns to x and y.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(8)
height = rng.normal(170, 9, 300)
df = pd.DataFrame({"height": height,
                   "weight": 0.9 * height - 85 + rng.normal(0, 7, 300),
                   "age": rng.uniform(18, 70, 300).round(),
                   "sex": rng.choice(["Female", "Male"], 300)})

df.plot.scatter(x = "height", y = "weight")
# plt.show()

Scatter plot in pandas

Color by a variable

The c argument colors the points by the values of a column. With a numeric column a color bar is added; choose the colors with colormap.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(8)
height = rng.normal(170, 9, 300)
df = pd.DataFrame({"height": height,
                   "weight": 0.9 * height - 85 + rng.normal(0, 7, 300),
                   "age": rng.uniform(18, 70, 300).round(),
                   "sex": rng.choice(["Female", "Male"], 300)})

df.plot.scatter(x = "height", y = "weight", c = "age", colormap = "viridis")
# plt.show()

Scatter plot colored by a numeric variable in pandas

Color by group

To color the points by a categorical variable map each group to a color.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(8)
height = rng.normal(170, 9, 300)
df = pd.DataFrame({"height": height,
                   "weight": 0.9 * height - 85 + rng.normal(0, 7, 300),
                   "age": rng.uniform(18, 70, 300).round(),
                   "sex": rng.choice(["Female", "Male"], 300)})

colors = {"Female": "#E76F51", "Male": "#264653"}

df.plot.scatter(x = "height", y = "weight", c = df["sex"].map(colors))
# plt.show()

Scatter plot colored by group in pandas

Size of the points

s sets the size of the points. Pass a number for a fixed size or a column to create a bubble chart.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(8)
height = rng.normal(170, 9, 300)
df = pd.DataFrame({"height": height,
                   "weight": 0.9 * height - 85 + rng.normal(0, 7, 300),
                   "age": rng.uniform(18, 70, 300).round(),
                   "sex": rng.choice(["Female", "Male"], 300)})

df.plot.scatter(x = "height", y = "weight", s = df["age"] * 2, alpha = 0.5)
# plt.show()

Scatter plot with variable point size in pandas

Scatter matrix

pandas.plotting.scatter_matrix creates a scatter plot for each pair of numeric columns and a histogram on the diagonal.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(8)
height = rng.normal(170, 9, 300)
df = pd.DataFrame({"height": height,
                   "weight": 0.9 * height - 85 + rng.normal(0, 7, 300),
                   "age": rng.uniform(18, 70, 300).round(),
                   "sex": rng.choice(["Female", "Male"], 300)})
from pandas.plotting import scatter_matrix

scatter_matrix(df[["height", "weight", "age"]], figsize = (5, 5), alpha = 0.5)
# plt.show()

Scatter matrix in pandas

Hexbin chart for many points

When there are many points they overlap. plot.hexbin groups them in hexagons colored by the number of observations.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(8)
height = rng.normal(170, 9, 300)
df = pd.DataFrame({"height": height,
                   "weight": 0.9 * height - 85 + rng.normal(0, 7, 300),
                   "age": rng.uniform(18, 70, 300).round(),
                   "sex": rng.choice(["Female", "Male"], 300)})

df.plot.hexbin(x = "height", y = "weight", gridsize = 15, cmap = "Blues")
# plt.show()

Hexbin chart in pandas

See also how to create a scatter plot in matplotlib or in seaborn and the plotting with pandas guide.

R CODER website
Data Sketches

Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon
Storytelling with Data

Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon

See also

Heatmap clustering in seaborn with clustermap

Heatmap clustering in seaborn with clustermap

Scatter plot in seaborn

Scatter plot in seaborn

Bubble chart in plotly

Bubble chart in plotly

Heatmaps in plotly with imshow

Heatmaps in plotly with imshow

Heat map in seaborn with the heatmap function

Heat map in seaborn with the heatmap function

Bubble chart in matplotlib

Bubble chart in matplotlib

Scatter plot by group in seaborn

Scatter plot by group in seaborn

2D histogram in matplotlib

2D histogram in matplotlib

Scatter plot in matplotlib

Scatter plot in matplotlib

Contour in matplotlib

Contour in matplotlib

You are visitor 01402792 — 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.