1

When using a notebook in Google Colab, my matplotlib plots have different behaviors whether I import the library pandas-profiling or not.

If I do not import pandas-profiling, the plots are displayed inline by default. But if I import the library, the plots stop being displayed inline.

Workarounds (possible solutions)

  • Updating the pandas-profiling library before importing it resolves the problem.
  • Adding %matplotlib inline after importing pandas-profiling resolves the problem.

Reproducing

Run this code in Google Colab to reproduce the problem. Test it with and without importing pandas_profiling. For each test, you will need to terminate the session (Runtime->Manage Sessions->Terminate). Just restarting the runtime is not enough.

import matplotlib.pyplot as plt
# importing the pandas_profiling makes matplotlib
# to stop showing the plot inline
# import pandas_profiling
plt.plot([1, 2], [1, 2])

The expected behavior is to show the plot inline by default, but after importing pandas-profiling, the plots stop being displayed inline.

The real problem

I stumbled upon this problem when my seaborn plotting functions started to break.

For example, consider the following code.

import matplotlib.pyplot as plt
# import pandas_profiling
import seaborn as sns

def plot():
    ax = sns.pointplot([1, 2], [1, 2])
    print(len(ax.collections))

Now call plot() in two different jupyter cells.

  • Without pandas-profiling: each function call will print 1.
  • With pandas-profiling: each function call will add 1 to the previous output.

1 Answers1

2

The problem is in the version of pandas_profiling that is installed by default in Google Colab. The installed version (1.4.1) used to change the matplotlib's backend to agg when imported. The default matplotlib's backend in Colab is module://ipykernel.pylab.backend_inline.

We can see that by running the following before and after importing pandas-profiling:

import matplotlib
matplotlib.get_backend()

This behavior was changed in pull-request #125, which stops changing matplotlib's backend.

Until Google Colab updates the installed version, manually updating it seems to be the best solution. Just run the following in your Colab Notebook:

!pip install --upgrade pandas_profiling