gpt4 book ai didi

python - Seaborn 热图阴谋失败

转载 作者:行者123 更新时间:2023-12-01 02:38:39 24 4
gpt4 key购买 nike

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

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

最佳答案

根据这个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)

关于python - Seaborn 热图阴谋失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45953128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com