Seaborns clustermap doesn't work with plt.tight_layout().
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(98)
df = pd.DataFrame(np.random.rand(10,10))
cm = sns.clustermap(df)
plt.tight_layout()
plt.show()
gives the following error ValueError: max() arg is an empty sequence
.
I've also used sns.heatmap()
with the exact same data set and this gave no errors. Is there a work around?
Note: The actual labels I'm using are a lot longer than the ones in this example but the codes too long to show. Thanks for any help!
I don't think tight_layout works with clustermap. However, from the sns.clustermap documentation, it says that: The returned object has a savefig method that should be used if you want to save the figure object without clipping the dendrograms.
tight_layout automatically adjusts subplot params so that the subplot (s) fits in to the figure area. This is an experimental feature and may not work for some cases. It only checks the extents of ticklabels, axis labels, and titles.
In general, subplots created from the gridspec ( Customizing Figure Layouts Using GridSpec and Other Functions) will work. Although not thoroughly tested, it seems to work for subplots with aspect != "auto" (e.g., axes with images). tight_layout considers all artists on the axes by default.
An alternative to tight_layout is constrained_layout. In matplotlib, the location of axes (including subplots) are specified in normalized figure coordinates. It can happen that your axis labels or titles (or sometimes even ticklabels) go outside the figure area, and are thus clipped. To prevent this, the location of axes needs to be adjusted.
I got this error when I placed the plt.tight_layout()
before the plt.plot()
and plt.xlabel()
I just had to move it to the last command before plt.show()
and it worked.
Another part of the error is the message:
This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect.
I don't think tight_layout
works with clustermap
. However, from the sns.clustermap
documentation, it says that:
The returned object has a savefig method that should be used if you want to save the figure object without clipping the dendrograms.
So, in your example, you could use cm.savefig('myfigure.png')
, which seems to perform similar actions to tight_layout
.
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