has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
4.06k stars 225 forks source link

Very weird interaction with PyCharm's SciView #137

Open Noctiphobia opened 6 years ago

Noctiphobia commented 6 years ago

When creating any plot with PlotNine and running it through IPython (6.2.1, Python 3.6.5) connected to PyCharm (2018.1.3 running on Linux 4.9.98-1-MANJARO #1 SMP PREEMPT Wed May 2 18:41:44 UTC 2018 x86_64 GNU/Linux) with SciView, more plots are drawn than expected. In particular, the last plot drawn with PlotNine is then drawn with every subsequent IPython call. For example after running the following code, 3 plots are drawn on SciView:

import plotnine as p9 import pandas as pd

df = pd.DataFrame({'x': list(range(10)), 'y': list(range(10))}) pl = p9.ggplot(data=df) + p9.geom_point(p9.aes(x='x', y='y')) pl2 = p9.ggplot(data=df) + p9.geom_point(p9.aes(x='x', y='y', color='x')) print(pl) print(pl2)

The first plot appears once, and the second appears twice (once due to print, and once due to the weird behavior I am describing). For every additional (unrelated) thing I do in IPython after calling the code above, I get another copy of the plot. For example:

In[3]: a = 1 # pl2 is drawn again in SciView In[4]: b = 2 # pl2 is drawn again In[5]: del pl2 # pl is drawn again In[6] c = 3 # pl is drawn again In[7] del pl # plots finally stop appearing

If I draw a plot using IPython's implicit printing (not assigning the plot to any variable), as far as I know, there is no way to delete it, so the plot will keep appearing until I reset IPython.

Just to clarify, this does happen only when using PyCharm's SciView. Running this through a separate IPython instance (not connected to Pycharm) works perfectly fine.

has2k1 commented 6 years ago

I do not use PyCharm, so I have no way of testing this, but it seems like SciView interferes with the cleaning up of matplotlib figures. IPython has set_matplotlib_close that closes all figures after each cell is run.

So, if you do this in IPython

import plotnine as p9
import pandas as pd
from IPython.core.display import set_matplotlib_close
set_matplotlib_close(False)

you should get unwanted plots showing up in there too!

tobiasdekkerschiphol commented 4 years ago

Does someone found a solution for this problem?

ckarras commented 4 years ago

I have this issue too and it's making plotnine unusable because every time I create a new plot, it also re-creates several other plots. There must be some way to reset plotnine's state to prevent this?

ckarras commented 4 years ago

I have also created an issue in PyCharm in case the bug is actually there: https://youtrack.jetbrains.com/issue/PY-42390

has2k1 commented 4 years ago

According to https://github.com/has2k1/plotnine/issues/397#issuecomment-627346430, SciView implements its own backend "interAgg" and that may be to cause of this.