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!
The size of a plot can be modified by passing required dimensions as a tuple to the figsize parameter of the plot() method. it is used to determine the size of a figure object.
In order to plot a histogram using pandas, chain the . hist() function to the dataframe. This will return the histogram for each numeric column in the pandas dataframe.
You can use . describe() to see a number of basic statistics about the column, such as the mean, min, max, and standard deviation. This can give you a quick overview of the shape of the data.
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()]
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))
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))
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With