jayko03
jayko03

Reputation: 2481

Pandas dataframe.hist() change title size on subplot?

I am manipulating DataFrame using pandas, Python. My data is 10000(rows) X 20(columns) and I am visualizing it, like this.

df.hist(figsize=(150,150))

However, if I make figsize bigger, each of subplots' title, which is name of each columns, get really small or graphs overlap each other and it makes impossible to distinguish.

Is there any clever way to fix it?

Thank you!

Upvotes: 7

Views: 18905

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339092

I would not recommend to make the figure much larger then 10 inch in each dimension. This should in any case be more than enough to host 20 subplots. And not making the figure so large will keep fontsize reasonable.
In order to prevent plot titles from overlappig, you may simply call plt.tight_layout().

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

df = pd.DataFrame(np.random.randn(1000,20))
df.hist(figsize=(10,9), ec="k")

plt.tight_layout()
plt.show()

enter image description here

Upvotes: 2

Zero
Zero

Reputation: 76917

There could be cleaner ways. Here are two ways.

1) You could set properties of subplots like

fig = df.hist(figsize=(50, 30))
[x.title.set_size(32) for x in fig.ravel()]

enter image description here

2) Another way, is to set matplotlib rcParams default parameters

import matplotlib

params = {'axes.titlesize':'32',
          'xtick.labelsize':'24',
          'ytick.labelsize':'24'}
matplotlib.rcParams.update(params)
df.hist(figsize=(50, 30))

enter image description here


Default Issue

This is default behavior with very small labels and titles in subplots.

matplotlib.rcParams.update(matplotlib.rcParamsDefault)  # to revert to default settings
df.hist(figsize=(50, 30))

enter image description here

Upvotes: 15

Related Questions