Open dominiquesydow opened 3 years ago
This worked in ipympl 0.8.0 but not in 0.8.2
so this may be something to do with the new display logic. Although interestingly just calling plt.gca
(which is what seaborn does) seems to work.
@mwaskom do you have any insight? it seems like you just call plt.gca()
but I suppose something that's happening in plotter.plot
is making things go poorly.
I think I'd want to know a few things
seaborn.heatmap
" problem?Thanks a lot for getting back to me so quickly! Let me try to apply your suggestions.
it seems like you just call
plt.gca()
but I suppose something that's happening inplotter.plot
is making things go poorly.
Do you mean like this?
%matplotlib widget
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df = pd.DataFrame([[10, 20, 30, 40], [50, 30, 8, 15],
[25, 14, 41, 8], [7, 14, 21, 28]])
ax = plt.gca()
sns.heatmap(df, ax=ax)
- If the latter, does a reasonably similar plot made only in matplotlib (i.e., pcolormesh with some texts on it) work?
Yes, it seems that works.
%matplotlib widget
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df = pd.DataFrame([[10, 20, 30, 40], [50, 30, 8, 15],
[25, 14, 41, 8], [7, 14, 21, 28]])
plt.pcolormesh(df)
for y in range(df.shape[0]):
for x in range(df.shape[1]):
plt.text(
x + 0.5, y + 0.5, "%.1f" % df.iloc[x, y],
horizontalalignment="center",
verticalalignment="center",
)
- If you can make a similar matplotlib plot with no issues, can you get the heatmap to work if you boil it down to the simplest version of what seaborn can draw? (i.e. no annotations, no colorbar, and disable the automatic selection of tick label density)
%matplotlib widget
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df = pd.DataFrame([[10, 20, 30, 40], [50, 30, 8, 15],
[25, 14, 41, 8], [7, 14, 21, 28]])
sns.heatmap(df)
The same behaviour with
sns.heatmap(df, xticklabels=False, yticklabels=False)
and
sns.heatmap(df, xticklabels=True, yticklabels=True)
assuming this is what was meant with
disable the automatic selection of tick label density
Yes that's right about the tick labels. But you could also try isolating the colorbar: it's on by default in the seaborn plot, so you could try turning it off there (cbar=False
and turning it on for the matplotlib plot (plt.colorbar
).
But I think that doesn't answer the other question which is ... can you use other seaborn functions, or is this an issue specific to heatmap
?
A crucial thing in these tests is probably to always start with plt.close('all')
so that the behavior of plt.gca
is consistent
Also @dominiquesydow can you please post the code snippets as text so that they are copy-pasteable - thanks!
Hi @ianhi,
Also @dominiquesydow can you please post the code snippets as text so that they are copy-pasteable - thanks!
I have updated my comment with copy-pasteable code.
Also, I have tried to investigate your initial question (my apologies for not getting to at first)
- Is this a "seaborn" problem or a "
seaborn.heatmap
" problem?
I set up this notebook; below a summary:
seaborn
plots seem fine to me.fig, ax = plt.subplots(nrows=1, ncols=1)
) and pass axis to seaborn
functions (ax
parameter)plt.close('all')
followed by ax = plt.gca()
)seaborn
heatmap tries to draw to the axis before the current axis (see the end of the posted notebook)
That behavior all looks correct to me. If you don't provide seaborn with an Axes
target, it plots on the "current" Axes (creating it if necessary). The Jupyter inline backend calls plt.close
behind the scenes after it executes every cell, but ipyml
doesn't (so e.g. you can create a plot in one cell then update it by doing something in another one).
I am having similar issue https://github.com/matplotlib/ipympl/issues/60#issuecomment-973722481
Hi @mwaskom,
Thanks for getting back to me. I am showing a minimal example here to show what is still confusing me.
# First cell
%matplotlib widget
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset("tips")
fig, ax = plt.subplots(nrows=1, ncols=1)
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time", ax=ax)
This works nicely, I get the plot:
Then, I call plt.close
as you suggested:
# Second cell
plt.close()
Then, I try to plot sns.heatmap
and get an empty plot:
# Third cell
import numpy as np
uniform_data = np.random.rand(10, 12)
fig, ax = plt.subplots(nrows=1, ncols=1)
sns.heatmap(uniform_data, ax=ax)
Do you have an idea on how to make the last plot appear?
If you don't provide seaborn with an Axes target, it plots on the "current" Axes (creating it if necessary). The Jupyter inline backend calls plt.close behind the scenes after it executes every cell, but ipyml doesn't (so e.g. you can create a plot in one cell then update it by doing something in another one).
With ax=ax
, I am providing sns.heatmap
with an Axes target (at least I think I am); I was expecting ipympl
to use that Axes as well.
I'm confused about how what you're describing there is not represented in the notebook you shared? You seem to be plotting heatmap
just fine there?
My apologies, apparently uploading the notebook to GH makes the heatmap appear... running the same notebook locally shows the behaviour discussed in this comment.
For the time being, I will pin ipympl
to 0.8.0; here the seaborn
heatmaps still appear as expected.
Oh that's interesting! I'd say this sounds like an issue on the ipympl side of things. I wish I could offer a hypothesis based on the fact that it seems to happen with heatmaps, but nothing comes immediately to mind beyond what I suggested above.
Thanks for taking the time to discuss this issue with me.
Although I cannot offer a solution with the latest ipympl
version, downgrading to 0.8.0 helped; I am therefore closing this issue. Please re-open if more discussion is needed for a solution for the latest version.
Please re-open if more discussion is needed for a solution for the latest version.
Yes this is definitely a bug than needs fixing - thanks for finding it!
Describe the issue
Thank you very much for providing this tool!
I have an apparently
seaborn
-related problem with showing widgets; I am posting this here first. Please let me know if you think this problem is rather onseaborn
's end and I'll repost it there.The problem:
seaborn
figures are not shown in JupyterLab with%matplotlib widget
(they do show up with%matplotlib inline
):No problem with
matplotlib
:Versions
Installed from conda-forge
More versions:
Do you have an idea why this is happening?