Seaborn热图失败 [英] Seaborn heatmap to plotly failed
问题描述
将seaborn.heatmap图转换为plotly时,出现plotly错误.我正在使用以下代码在jupyter笔记本中执行此操作:
I'm having plotly error when converting seaborn.heatmap figure to plotly. I'm doing that in jupyter notebook with following code:
%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)
当我将其绘制为静态海洋热图时,一切都很好:
When I'm plotting it as static seaborn heatmap everything is alright:
sns.heatmap(data)
但是当我尝试将该对象转换为绘图时,我遇到了一个错误:
But when I'm trying to convert that object to plotly I have an error:
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
推荐答案
根据此示例 ,您首先需要将matplotlib
图形转换为Plotly
对象,然后手动添加data
.从一开始,这是否比使用Plotly
进行所有操作更方便?
According to this example, you would need to first convert your matplotlib
figure to a Plotly
object and then add the data
manually. Whether that's more convenient than doing everything with Plotly
from the beginning is a different issue.
%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)
这篇关于Seaborn热图失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!