2017-08-30 141 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地塊嗎?我有一個很大的熱圖,有很多左列的字符串。 –