Repository: ydataai/ydata-profiling

Issue:
This call to matplotlib.use() has no effect because the backend has already
/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/pandas_profiling/base.py:20: UserWarning: 
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

The backend was *originally* set to 'module://ipykernel.pylab.backend_inline' by the following code:
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-47d9d0ad501d>", line 8, in <module>
    import matplotlib.pyplot as plt
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 69, in <module>
    from matplotlib.backends import pylab_setup
  File "/home/flash1/work/software/python/anaconda2/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 14, in <module>
    line for line in traceback.format_stack()


  matplotlib.use('Agg')


Discussion:
Hello,

Could you please explain your use case and take the time to format the issue?

Many thanks
+1 I have this issue. I've narrowed it down to:
```
import seaborn
import pandas_profiling
# only these two lines will generate:
/home/ian/anaconda3/envs/data_science_delivered/lib/python3.6/site-packages/pandas_profiling/base.py:20: UserWarning: 
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

The backend was *originally* set to 'module://ipykernel.pylab.backend_inline' by the following code:
  File "/home/ian/anaconda3/envs/data_science_delivered/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
...
    from matplotlib.backends import pylab_setup
  File "/home/ian/anaconda3/envs/data_science_delivered/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 14, in <module>
    line for line in traceback.format_stack()

  matplotlib.use('Agg')
```

My versions (using `watermark`):
```
2017-11-15 

CPython 3.6.2
IPython 6.1.0

numpy 1.13.3
matplotlib 2.1.0
seaborn 0.8.0

compiler   : GCC 4.8.2 20140120 (Red Hat 4.8.2-15)
system     : Linux
release    : 4.9.45-040945-generic
machine    : x86_64
processor  : x86_64
CPU cores  : 8
interpreter: 64bit
```

If I invert those lines (so seaborn is second) then the warning goes away, if I do either combo in my _real_ import list then I still get the huge warning:
```
import datetime
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pandas_profiling
import seaborn as sns
import tqdm

import eli5 # https://github.com/TeamHG-Memex/eli5 via conda
import lime # https://github.com/marcotcr/lime via PyPI (note this was built using MASTER at time-of-creation)
import yellowbrick # https://github.com/DistrictDataLabs/yellowbrick/ via conda
from eli5 import show_weights
from eli5 import show_prediction

import lime.lime_tabular
from matplotlib import pyplot as plt
%matplotlib inline
```

Why does `base.py` set `matplotlib.use('Agg')`? Doesn't that depend on the user's requirements?

I'll note that if comment-out `matplotlib.use('Agg')` in `base.py` - this problem goes away. Maybe that's the easy fix?

I _think_ in my Notebook that the line is redundant as I'm using the Jupyter backend, my first few lines are:
```
import matplotlib
print(matplotlib.get_backend())
-> module://ipykernel.pylab.backend_inline
```
and when I subsequently import `pandas_profiling` the backend doesn't change (with the Agg line enabled in `base.py`).

@iweey are you using a Jupyter Notebook too? Your error looks to be the same as mine?
@ianozsvald  yes,Jupyter Notebook meet this issue 
I'll note that I've ignored this warning using:
```
import warnings
warnings.filterwarnings('ignore') 
```
over here: https://github.com/ianozsvald/simple_generative_insurance_model/blob/master/simple_generative_insurance_model.ipynb

This is a blunt-stick approach, commenting out `matplotlib.use('Agg')` in `base.py` works fine too and I do wonder if that needs to be set by `pandas_profiling` as it feels user-machine-specific?
Hello, 

Thanks to all for your comments ! Here is a [link](http://matplotlib.org/faq/usage_faq.html#what-is-a-backend) to the reference documentation on backend.

> If you use the use() function, this must be done before importing `matplotlib.pyplot`. Calling `use()` after `pyplot` has been imported will have no effect. Using `use()` will require changes in your code if users want to use a different backend. Therefore, you should avoid explicitly calling `use()` unless absolutely necessary.

So you may be right, the explicit call to `use` may be useless. However my guess is that it has been made to ensure that figures in the report are generated in PNG format. 
I will dig in to find if it is necessary in all contexts, I have also to check the behavior when the tool is run outside Jupyter (call to `profile_csv.py`). And if so, maybe there is a better way to handle it when it's already set.

Best.


Hey guys!

I commented the `matplotlib.use('Agg')` line and tested the `profile_csv.py` and jupyter notebook. Everything seems to work without it. I think we should remove it, I can do an PR if necessary.

Best.

Resolving patch:
diff --git a/pandas_profiling/base.py b/pandas_profiling/base.py
--- a/pandas_profiling/base.py
+++ b/pandas_profiling/base.py
@@ -17,7 +17,8 @@
 import base64
 
 import matplotlib
-matplotlib.use('Agg')
+# Fix #68, this call is not needed and brings side effects in some use cases
+# matplotlib.use('Agg')
 
 import numpy as np
 import pandas as pd


Test patch: