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 — Histogram in pandas

Search for a graph

HOME DISTRIBUTION HISTOGRAM PANDAS

Histogram in pandas

pandas provides two ways to create histograms: plot.hist, which draws all the columns in the same chart, and hist, which creates a grid with one histogram per column.

Histogram with plot.hist

Call plot.hist on a Series to draw its histogram. The bins argument sets the number of intervals (10 by default).

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

rng = np.random.default_rng(5)
df = pd.DataFrame({"math": rng.normal(65, 12, 300).clip(0, 100),
                   "physics": rng.normal(55, 15, 300).clip(0, 100),
                   "school": rng.choice(["North", "South", "East"], 300)})

df["math"].plot.hist(bins = 20, edgecolor = "white")
# plt.show()

Histogram in pandas with plot.hist

Several histograms in the same chart

On a data frame, plot.hist draws one histogram per column in the same chart. Set some transparency with alpha so all of them are visible, or stack them with stacked = True.

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

rng = np.random.default_rng(5)
df = pd.DataFrame({"math": rng.normal(65, 12, 300).clip(0, 100),
                   "physics": rng.normal(55, 15, 300).clip(0, 100),
                   "school": rng.choice(["North", "South", "East"], 300)})

df[["math", "physics"]].plot.hist(bins = 20, alpha = 0.5)
# plt.show()

Overlapping histograms in pandas

Stacked histogram

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

rng = np.random.default_rng(5)
df = pd.DataFrame({"math": rng.normal(65, 12, 300).clip(0, 100),
                   "physics": rng.normal(55, 15, 300).clip(0, 100),
                   "school": rng.choice(["North", "South", "East"], 300)})

df[["math", "physics"]].plot.hist(bins = 20, stacked = True)
# plt.show()

Stacked histogram in pandas

One histogram per column with hist

The hist method creates a grid of subplots with the histogram of each numeric column.

One histogram per column with df.hist in pandas

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

rng = np.random.default_rng(5)
df = pd.DataFrame({"math": rng.normal(65, 12, 300).clip(0, 100),
                   "physics": rng.normal(55, 15, 300).clip(0, 100),
                   "school": rng.choice(["North", "South", "East"], 300)})

df.hist(bins = 20, figsize = (8, 3.5))
# plt.show()

Histogram by group

The by argument of hist creates one histogram for each group of a categorical variable.

Histogram by group in pandas

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

rng = np.random.default_rng(5)
df = pd.DataFrame({"math": rng.normal(65, 12, 300).clip(0, 100),
                   "physics": rng.normal(55, 15, 300).clip(0, 100),
                   "school": rng.choice(["North", "South", "East"], 300)})

df.hist(column = "math", by = "school", bins = 15, layout = (1, 3),
        sharex = True, figsize = (8, 3))
# plt.show()

Histogram with density curve

Use density = True to show the histogram as proportions and add the kernel density estimate with plot.kde on the same axes.

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

rng = np.random.default_rng(5)
df = pd.DataFrame({"math": rng.normal(65, 12, 300).clip(0, 100),
                   "physics": rng.normal(55, 15, 300).clip(0, 100),
                   "school": rng.choice(["North", "South", "East"], 300)})

ax = df["math"].plot.hist(bins = 20, density = True, alpha = 0.5)
df["math"].plot.kde(ax = ax)
# plt.show()

Histogram with density curve in pandas

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

R CODER website
Better Data Visualizations

Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Fundamentals of Data Visualization

Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon

See also

Violin plots in plotly

Violin plots in plotly

Histogram with density in seaborn

Histogram with density in seaborn

Box plot in matplotlib

Box plot in matplotlib

Violin plot in matplotlib

Violin plot in matplotlib

Violin plot in seaborn

Violin plot in seaborn

Histogram in matplotlib

Histogram in matplotlib

Kernel density plot in seaborn with kdeplot

Kernel density plot in seaborn with kdeplot

Swarm plot in seaborn

Swarm plot in seaborn

Histogram by group in seaborn

Histogram by group in seaborn

Ridgeline plots (joy plots) in matplotlib with joypy

Ridgeline plots (joy plots) in matplotlib with joypy

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.