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 — Box plot in pandas

Search for a graph

HOME DISTRIBUTION BOX PLOT PANDAS

Box plot in pandas

Box plots summarize the distribution of a variable with its quartiles and show the outliers. In pandas they are created with plot.box or with the boxplot method, which also allows creating one box for each group.

Box plot with plot.box

plot.box draws one box for each numeric column of the data frame.

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

rng = np.random.default_rng(6)
df = pd.DataFrame({"standard": rng.gamma(6, 7, 300),
                   "express": rng.gamma(4, 5, 300),
                   "city": rng.choice(["Madrid", "Barcelona", "Seville"], 300)})

df[["standard", "express"]].plot.box()
# plt.show()

Box plot of several columns in pandas

Horizontal box plot

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

rng = np.random.default_rng(6)
df = pd.DataFrame({"standard": rng.gamma(6, 7, 300),
                   "express": rng.gamma(4, 5, 300),
                   "city": rng.choice(["Madrid", "Barcelona", "Seville"], 300)})

df[["standard", "express"]].plot.box(vert = False)
# plt.show()

Horizontal box plot in pandas

Box plot by group

The boxplot method with the by argument creates a box for each group of a categorical variable.

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

rng = np.random.default_rng(6)
df = pd.DataFrame({"standard": rng.gamma(6, 7, 300),
                   "express": rng.gamma(4, 5, 300),
                   "city": rng.choice(["Madrid", "Barcelona", "Seville"], 300)})

df.boxplot(column = "standard", by = "city", grid = False)
plt.suptitle("")
# plt.show()

Box plot by group in pandas

pandas adds an automatic title to the figure when using by. Remove it with plt.suptitle("") as in the previous example.

Colors of the box plot

Pass a dictionary to color to set the color of the boxes, whiskers, medians and caps. Use patch_artist = True to fill the boxes.

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

rng = np.random.default_rng(6)
df = pd.DataFrame({"standard": rng.gamma(6, 7, 300),
                   "express": rng.gamma(4, 5, 300),
                   "city": rng.choice(["Madrid", "Barcelona", "Seville"], 300)})

df[["standard", "express"]].plot.box(color = {"boxes": "#264653", "whiskers": "#264653",
                                               "medians": "#E76F51", "caps": "#264653"},
                                      patch_artist = True)
# plt.show()

Change the colors of a pandas box plot

Show the mean

Any argument of the matplotlib boxplot function can be passed to pandas. For instance, showmeans = True adds the mean of each box.

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

rng = np.random.default_rng(6)
df = pd.DataFrame({"standard": rng.gamma(6, 7, 300),
                   "express": rng.gamma(4, 5, 300),
                   "city": rng.choice(["Madrid", "Barcelona", "Seville"], 300)})

df[["standard", "express"]].plot.box(showmeans = True)
# plt.show()

Box plot with the mean in pandas

Learn how to create a box plot in matplotlib or in seaborn for more customization options.

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

Histogram with density in seaborn

Histogram with density in seaborn

Histogram by group in seaborn

Histogram by group in seaborn

Strip plot in seaborn

Strip plot in seaborn

Box plot in matplotlib

Box plot in matplotlib

Histogram in pandas

Histogram in pandas

Histogram in seaborn with histplot

Histogram in seaborn with histplot

Box plot in seaborn

Box plot in seaborn

Kernel density plot in seaborn with kdeplot

Kernel density plot in seaborn with kdeplot

Violin plot in matplotlib

Violin plot in matplotlib

Violin plots in plotly

Violin plots in plotly

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.