-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Long axis title alters xaxis length and direction with plt.tight_layout()
#4413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
plt.tight_layout()
plt.tight_layout()
Can you reproduce this with out facet grid? Please include the code to reproduce the bug in the thread here rather than external link (which will rot). |
Yeah I had an example with matplotlib further down in the notebook. I'm including the relevant code in this post also. import seaborn.apionly as sns
import matplotlib.pyplot as plt
%matplotlib inline
iris = sns.load_dataset('iris')
#plotting with different title lengths
#narrower axis
fig, axes = plt.subplots(1,3, figsize=(8,4))
for ax in axes.ravel():
ax.hist(iris.petal_length)
ax.set(title='long_title' * 5 )
sns.despine()
plt.tight_layout()
#reversed axis
fig, axes = plt.subplots(1,3, figsize=(8,4))
for ax in axes.ravel():
ax.hist(iris.petal_length)
ax.set(title='long_title' * 10 )
sns.despine()
plt.tight_layout()
#throws an error if you uncomment `plt.tight_layout()`
fig, axes = plt.subplots(1,3, figsize=(8,4))
for ax in axes.ravel():
ax.hist(iris.petal_length)
ax.set(title='long_title' * 20 )
sns.despine()
#plt.tight_layout() Below is the output, the last graph looks normal because I comment the |
It looks like what is happening is that the right edge of the first axes is getting pushed to match the left edge of the center title and the titles are always centered over the axes. I am not sure what the correct behavior here is. I suspect you would want the title to wrap which we have a PR in to add (#4342). |
The flipping the axis is an |
@jklymak I just ran the code without Narrow x-axisimport seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
iris = sns.load_dataset('iris')
# Plotting with different title lengths
# narrower axis
fig, axes = plt.subplots(1,3, figsize=(8,4))
for ax in axes.ravel():
ax.hist(iris.petal_length)
ax.set(title='long_title' * 5 )
plt.tight_layout() Reversed x-axis# reversed axis
fig, axes = plt.subplots(1,3, figsize=(8,4))
for ax in axes.ravel():
ax.hist(iris.petal_length)
ax.set(title='long_title' * 8 )
plt.tight_layout() # without `plt.tight_layout()` the axis is not reversed
fig, axes = plt.subplots(1,3, figsize=(8,4))
for ax in axes.ravel():
ax.hist(iris.petal_length)
ax.set(title='long_title' * 8 )
# plt.tight_layout() Error message for long titlefig, axes = plt.subplots(1,3, figsize=(8,4))
for ax in axes.ravel():
ax.hist(iris.petal_length)
ax.set(title='long_title' * 15 )
plt.tight_layout() Output:
|
This is totally independent on seaborn and indeed only due to
is indeed correct. There does not seem to be a canonical way to solve this though. Options could be:
None of those seem very appealing. And one may also just leave it to the user to take care of not using too long titles. There is this sentence in the tight_layout guide
which seems to suggest exactly this. Maybe it should just be written more comprehensively? |
Is |
It's not negative numbers per se, but rather the Code for reproduction
But where would you draw the line, is a |
|
Yep, but it's not like you would run this kind of continuous checking when doing |
This is a pretty easy check. PR coming. What is the preferred failure behaviour?
|
It's all less than ideal. I suppose it is impossible to find out if it fails due to a long title or due to e.g. too long axis labels (which I suppose would show a similar issue?)? Thinking about it, from a user's perspective it would make most sense to go with the "leave out the title width completely from the adjustments" option. That would then allow to have plots like So if it is possible to find out that it's the title that causes the problem I would issue a warning informing the user about the title-problem and even suggesting to first call tight_layout and only after that set the title. |
Thanks for spending time on this! From a user's perspective, I agree that option 1 would be the most helpful as per the reasons laid out by @ImportanceOfBeingErnest above (perhaps also indicating that the plot appearance will be affected by this). If the long title could be pinpointed as the underlying issue that would be even better, but I don't think it's a big deal if it is cumbersome/not possible to implement (maybe it could just be suggested as a possible underlying issue?). |
Please see #10915 It mostly does 1, but the axes doesn't go to zero width... |
I originally reported this as a seaborn issue, but it turns out that it is the
tight_layout
property that causes the xaxis to get narrower, then reverse and eventually throw an error if the axis title is too long. See this example notebook for details using bothmatplotlib
andseaborn
.As I mentioned on the seaborn page, I don't think this is especially important and it's easy for the user to adjust by shrinking or splitting the title. I was just confused initially when my plots changed direction on the axis and didn't immediately relate it to the title length. Maybe a warning message can pop up when the axis title is very long, saying that it might have unexpected effects on the plot appearance?
The text was updated successfully, but these errors were encountered: