【问题标题】:Is it possible to suppress Matplotlib/Seaborn output in Jupyter notebook?是否可以在 Jupyter 笔记本中抑制 Matplotlib/Seaborn 输出?
【发布时间】:2020-09-15 20:07:12
【问题描述】:

所有 - 寻求一些指导(如果有的话),以帮助解决这个令人沮丧的问题。

我正在定义一个函数,该函数使用 Seaborn 的带注释热图创建相关矩阵。该函数工作正常,但是,Seaborn 输出会在运行该函数时自动生成,而无需调用它。

我希望抑制此输出,仅在稍后调用相关矩阵时在笔记本中生成它,就像您对数据框、其他图表等所做的那样。

有什么解决办法吗?到目前为止,我已经尝试向图形/轴对象添加分号、put.ioff()、不同的分配。老实说,我不确定这是 Seaborn 问题还是与 Matplotlib 相关的问题。也许可以用另一种方式编写函数来减轻这个限制?

代码如下。对此的任何帮助将不胜感激,非常感谢。

# cormat is a correlation matrix

import matplotlib.pyplot as plt

def correl_heatmap(cormat):
    _f01, ax = plt.subplots(figsize=(cormat.shape[0], cormat.shape[0]));
    sns.heatmap(cormat,
                vmin=-1, vmax=1, center=0, square=True, 
                annot=True, cmap='coolwarm_r', cbar_kws={'shrink': 0.8}, ax=ax);
    ax.set_xticklabels(ax.get_xticklabels(),
                       rotation=90,
                       horizontalalignment='center');
    ax.set_yticklabels(ax.get_yticklabels(),
                       rotation=90,
                       verticalalignment='center');
    return _f01



_f01 = correl_heatmap(cormat)
_f01

【问题讨论】:

标签: python matplotlib seaborn jupyter


【解决方案1】:

要抑制此输出,请为返回对象指定名称:

_ = plt.plot(A)

plot(A);

【讨论】:

  • 感谢您的回复。我已经把它分配给_f01了,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
  • 2018-01-12
  • 2016-02-24
  • 2016-10-30
相关资源
最近更新 更多