How do you Change the Figure Size of a Pandas Histogram?

The figure size of a Pandas histogram can be changed by using the figsize parameter when creating the histogram. This parameter takes a tuple of (width, height) values to specify the size of the figure. Changing the figure size allows users to adjust the space the histogram takes up on the plot. For example, the following code creates a histogram with a width of 8 and a height of 6: plt.figure(figsize=(8,6)) plt.hist(data).


You can use the figsize argument to change the figure size of a histogram created in pandas:

import matplotlib.pyplot as plt

#specify figure size (width, height)
fig = plt.figure(figsize=(8,3))
ax = fig.gca()

#create histogram using specified figure size
df['my_column'].hist(ax=ax)

The following example shows how to use the figsize argument in practice.

Example: How to Change Figure Size of Pandas Histogram

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                              'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'],
                   'points': [10, 12, 14, 15, 15, 15, 16, 17,
                              19, 19, 24, 24, 28, 30, 34, 34]})

#view first five rows of DataFrame
print(df.head())

  player  points
0      A      10
1      B      12
2      C      14
3      D      15
4      E      15

If we create a histogram for the points variable, pandas will automatically use 6.4 as the width of the figure and 4.8 as the height:

import matplotlib.pyplot as plt

#create histogram for points variable
df['points'].hist(grid=False, edgecolor='black')

However, we can use the figsize argument to change the width and height of the figure:

import matplotlib.pyplot as plt

#specify figure size (width, height)
fig = plt.figure(figsize=(8,3))
ax = fig.gca()

#create histogram using specified figure size
df['points'].hist(grid=False, edgecolor='black', ax=ax)

This particular histogram has a width of 8 and a height of 3.

We can also use the figsize argument to create a figure that has a greater height than width:

import matplotlib.pyplot as plt

#specify figure size (width, height)
fig = plt.figure(figsize=(4,7))
ax = fig.gca()

#create histogram using specified figure size
df['points'].hist(grid=False, edgecolor='black', ax=ax)

This particular histogram has a width of 4 and a height of 7.

x