Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the space between histograms in pandas

Tags:

python

pandas

I'm currently using df.hist(alpha = .5), but all of the subplots are too close from each other, like this:

Histograms

Which way is better to change the space between them? Or is better to plot each one in a separate .png file?

like image 544
Biel Borba Avatar asked Dec 06 '22 11:12

Biel Borba


1 Answers

One simple way is to manipulate figsize and add pyplot.tight_layout. Below is the example.

Without adjustment:

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

df = pd.DataFrame(np.random.randn(6400)
                  .reshape((100, 64)), columns=['col_{}'.format(i) for i in range(64)])
df.hist(alpha=0.5)
plt.show()

You will get this as you showed:

enter image description here

In contrast, if you add figsize (with arbitrary size) and pyplot.tight_layout like below:

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

df = pd.DataFrame(np.random.randn(6400)
                  .reshape((100, 64)), columns=['col_{}'.format(i) for i in range(64)])
df.hist(alpha=0.5, figsize=(20, 10))
plt.tight_layout()
plt.show()

In this case you will get more aligned view:

enter image description here

Hope this helps.

like image 149
gyoza Avatar answered Dec 30 '22 09:12

gyoza