简体   繁体   中英

Seaborn heatmap to plotly failed

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)

Error Traceback:

/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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM