displot does not show part of the title and xlabel

692 Views Asked by At

This is a snippet of my code:

import pandas as pd
import seaborn as sns

filename = 'MY_FILENAME_GOES_HERE'

enctimes=pd.read_csv(filename,skiprows=10)

sns.displot(enctimes,kde=True, legend=False, bins=bins).set(title='Encoding Times  %s, number frames %d' %(fileForTitle,len(enctimes)),xlabel='usec')

plt.show()

Why does it cut off the title and how do I fix it?

enter image description here

2

There are 2 best solutions below

0
On

You can use plt.tight_layout (see Tight Layout guide) to fit the title automatically. Here is a sample edit of your code snippet:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

enctimes = np.random.normal(size=840)
bins = 30
title = 'Encoding Times %s, number frames %d' % ('fileForTitle', len(enctimes))
sns.displot(enctimes, kde=True, legend=False, bins=bins).set(title=title, xlabel='usec')
plt.tight_layout()
plt.show()

Executing this will output:

output

0
On

JohanC recommended to use "plt.tight_layout()", i added this function before plt.show() and now the title is not cut

enter image description here