2017-08-30 223 views
1

将seaborn.heatmap图转换为图形时,我出现了密谋错误。我这样做,在jupyter笔记本采用下面的代码:Seaborn热图出现故障

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

enter image description here

但是,当我试图给该对象转换为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 

回答

2

根据这一example,你需要首先你matplotlib图转换为Plotly对象,然后手动添加data。从一开始就用Plotly做任何事都更方便,这是另一回事。

enter image description here

%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) 
+0

感谢您的回答。我看到了这个例子,但它不适合我。我试过你的,它给出了一个错误: ''/opt/cloudera/parcels/Anconda/envs/py35/lib/python3.5/site-packages/plotly/matplotlylib/renderer.py:445:UserWarning: 当!那个路径集合超出了这个世界。我完全不知道该怎么办呢! Plotly只能导入链接到'data'坐标的路径集合''' 并没有显示任何内容。 –

+0

我得到相同的错误/警告,但可以继续工作。我没有用Anaconda,而是用香草Python/Jupyter来试用它。 –

+0

但是,它可以很好地用'plot'来存档。你知道蜱,斧头命名出口从matplotlib/seaborn地块吗?我有一个很大的热图,有很多左列的字符串。 –