【问题标题】:Google Colab: Why matplotlib has a behaviour different then default after importing pandas_profiling?Google Colab:为什么 matplotlib 在导入 pandas_profiling 后的行为与默认行为不同?
【发布时间】:2020-02-28 05:27:13
【问题描述】:

在 Google Colab 中使用笔记本时,无论我是否导入库 pandas-profiling,我的 matplotlib 绘图都有不同的行为。

如果我不导入 pandas-profiling,这些图会默认显示为内联。但是,如果我导入库,这些图停止内联显示

解决方法(可能的解决方案)

  • 在导入之前更新 pandas-profiling 库可以解决问题。
  • 添加%matplotlib inline导入pandas-profiling后可以解决问题。

复制

在 Google Colab 中运行此代码以重现该问题。在导入和不导入 pandas_profiling 的情况下对其进行测试。对于每个测试,您将需要终止会话(运行时->管理会话->终止)。仅仅重启运行时是不够的。

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])

expected behavior默认是内联显示绘图,但是导入pandas-profiling后,绘图stop being displayed inline

真正的问题

当我的 seaborn 绘图功能开始出现故障时,我偶然发现了这个问题。

例如,考虑以下代码。

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))

现在在两个不同的 jupyter 单元中调用 plot()

  • 没有 pandas-profiling:每个函数调用都会打印 1。
  • 使用 pandas-profiling:每个函数调用都会在之前的输出中加 1。

【问题讨论】:

    标签: matplotlib seaborn google-colaboratory pandas-profiling


    【解决方案1】:

    问题出在 Google Colab 中默认安装的 pandas_profiling 版本中。已安装的版本 (1.4.1) 用于在导入时将 matplotlib 的后端更改为 agg。 Colab 中默认 matplotlib 的后端是 module://ipykernel.pylab.backend_inline

    我们可以通过在导入 pandas-profiling 前后运行以下命令来看到:

    import matplotlib
    matplotlib.get_backend()
    

    此行为在 pull-request #125 中已更改,它停止更改 matplotlib 的后端。

    在 Google Colab 更新已安装的版本之前,手动更新似乎是最好的解决方案。只需在您的 Colab Notebook 中运行以下命令:

    !pip install --upgrade pandas_profiling
    

    【讨论】:

      猜你喜欢
      • 2012-09-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      相关资源
      最近更新 更多