Seaborn 热图阴谋失败

Posted

技术标签:

【中文标题】Seaborn 热图阴谋失败【英文标题】:Seaborn heatmap to plotly failed 【发布时间】:2018-02-07 17:42:41 【问题描述】:

将 seaborn.heatmap 图形转换为 plotly 时,我遇到了 plotly 错误。我正在使用以下代码在 jupyter 笔记本中执行此操作:

%matplotlib inline
import numpy as np

import seaborn as sns
import matplotlib.pyplot as plt
from plotly.offline import init_notebook_mode, iplot_mpl

init_notebook_mode(connected=True)

np.random.seed(2017)
data = np.random.randn(10, 20)

当我将其绘制为静态 seaborn 热图时,一切都很好:

sns.heatmap(data)

但是当我尝试将该对象转换为 plotly 时,我遇到了一个错误:

sns.heatmap(data)
fig = plt.gcf()
iplot_mpl(fig)

错误回溯:

/opt/cloudera/parcels/Anaconda/envs/py35/lib/python3.5/site-packages/plotly/matplotlylib/renderer.py:445: UserWarning:

Dang! That path collection is out of this world. I totally don't know what to do with it yet! Plotly can only import path collections linked to 'data' coordinates

---------------------------------------------------------------------------
PlotlyEmptyDataError                      Traceback (most recent call last)
<ipython-input-3-2a07e9adfc34> in <module>()
      1 sns.heatmap(data)
      2 fig = plt.gcf()
----> 3 iplot_mpl(fig)

/opt/cloudera/parcels/Anaconda/envs/py35/lib/python3.5/site-packages/plotly/offline/offline.py in iplot_mpl(mpl_fig, resize, strip_style, verbose, show_link, link_text, validate, image, image_filename, image_height, image_width)
    681     return iplot(plotly_plot, show_link, link_text, validate,
    682                  image=image, filename=image_filename,
--> 683                  image_height=image_height, image_width=image_width)
    684 
    685 

/opt/cloudera/parcels/Anaconda/envs/py35/lib/python3.5/site-packages/plotly/offline/offline.py in iplot(figure_or_data, show_link, link_text, validate, image, filename, image_width, image_height, config)
    330     config.setdefault('linkText', link_text)
    331 
--> 332     figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
    333 
    334     # Though it can add quite a bit to the display-bundle size, we include

/opt/cloudera/parcels/Anaconda/envs/py35/lib/python3.5/site-packages/plotly/tools.py in return_figure_from_figure_or_data(figure_or_data, validate_figure)
   1396         if not figure['data']:
   1397             raise exceptions.PlotlyEmptyDataError(
-> 1398                 "Empty data list found. Make sure that you populated the "
   1399                 "list of data objects you're sending and try again.\n"
   1400                 "Questions? Visit support.plot.ly"

PlotlyEmptyDataError: Empty data list found. Make sure that you populated the list of data objects you're sending and try again.
Questions? Visit support.plot.ly

【问题讨论】:

【参考方案1】:

根据这个example,您需要先将matplotlib 图形转换为Plotly 对象,然后手动添加data。这是否比从一开始就使用Plotly 做所有事情更方便是另一个问题。

%matplotlib inline

import plotly
import matplotlib as plt
import numpy as np
import seaborn as sns

plotly.offline.init_notebook_mode()

np.random.seed(2017)

data = np.random.randn(10, 20)
sns.heatmap(data)

mpl_fig = plt.pyplot.gcf()
plotly_fig = plotly.tools.mpl_to_plotly(mpl_fig)
plotly_fig['data'] = [dict(z=data, type="heatmap")]
plotly.offline.iplot(plotly_fig)

【讨论】:

感谢您的回答。我看到了那个例子,但它对我不起作用。我试过你的,它给出了一个错误:/opt/cloudera/parcels/Anaconda/envs/py35/lib/python3.5/site-packages/plotly/matplotlylib/renderer.py:445: UserWarning: Dang! That path collection is out of this world. I totally don't know what to do with it yet! Plotly can only import path collections linked to 'data' coordinates 什么都没有显示.. 我收到相同的错误/警告,但可以继续工作。我没有尝试使用 Anaconda,而是使用香草 Python/Jupyter。 但是,它与 plot 一起工作正常。您知道从 matplotlib/seaborn 图中导出的刻度、轴命名吗?我有一个大热图,左列有很多字符串。

以上是关于Seaborn 热图阴谋失败的主要内容,如果未能解决你的问题,请参考以下文章

Seaborn 热图相关性不适合注释数字

样式化 Seaborn 和 Plotly 热图

Seaborn 热图:在绘图顶部移动颜色条

Matplotlib / Seaborn:热图的控制线/行高

如何修复 matplotlib 和 seaborn 热图?

seaborn 热图图中的离散图例