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.
Feel free to play around with the values in the figsize argument to create a histogram with the exact size you’d like.
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
How to Create a Histogram from Pandas DataFrame
How to Create a Histogram from a Pandas Series
How to Plot Histograms by Group in Pandas