【问题标题】:Convert a matplotlib pie chart to plotly dictionary将 matplotlib 饼图转换为 plotly 字典
【发布时间】:2017-08-23 06:49:24
【问题描述】:

当我尝试将 MatPlotLib 生成的饼图转换为 Plotly 字典时,出现以下错误:PlotlyEmptyDataError: Empty data list found。确保您填写了要发送的数据对象列表,然后重试。

确实,如果我执行 plotly_fig(请参阅下面的脚本),我会得到空数据:

{'data': [],
 'layout': {'annotations'...etc.

我找不到饼图的限制,所以我不确定我是否做错了什么?

    #import pylab 
import matplotlib.pyplot as plt
import matplotlib as mpl 
import plotly 
# Plotly
import plotly.plotly as py
import plotly.tools as tls
import plotly.plotly as py
import plotly.graph_objs as go
plotly.offline.init_notebook_mode()

mpl.rcParams['font.size'] = 7
fig, ax = plt.subplots()
l = ax.pie([1,0.2, 0.3,0.1,0.4,7,50,35,5,2],#[0, 0.1, 0, 0.1,0,0.1,0,0.1,0,0.1],
           labels=("one","two","three made up sentences","four is also ther","five becomes a sentence","six it is","seven long", "eight long sent", "nine, as bla bel mo","ten is also short"),
            #labels=("","","","","","six it is","seven long", "eight long sent", "nine, as bla bel mo","ten is also short"),
            colors=("b","g","r","y", "b","g","r","y","g","black"),
            startangle =20,
            radius=1,
            explode = [0.2, 0.2, 0.2,  0.2, 0.2, 0.02, 0.02, 0.02, 0.02, 0.02 ],
            frame=True,   # Plot axes frame with the chart if true.
            labeldistance = 1.1 ) #returns a list of matplotlib.patches.Wedge objects
#l_in = ax.pie([1,0.2, 0.3,0.1,0.4,7,50,35,5,2],#[0, 0.1, 0, 0.1,0,0.1,0,0.1,0,0.1],
#            colors=("w","w","w","w", "w","w","w","w","w","w"),
#            startangle =20,
#            radius=0.4,
#            frame=True)   

l2 = ax.pie([1,0.2, 0.3,0.1,0.4,7,50,35,5,2],#[0, 0.1, 0, 0.1,0,0.1,0,0.1,0,0.1],
            colors=("r","g","b","w", "g","b","y","r","w","black"),
            explode = [0.2, 0.2, 0.2,  0.2, 0.2, 0.02, 0.02, 0.02, 0.02, 0.02 ],
            startangle =20,
            radius=1-0.7,
            frame=True)   # Plot axes frame with the chart if true.
ax.axis('equal')

plotly_fig = tls.mpl_to_plotly(fig)
fig = go.Figure(data=data, layout=layout)    # help(go.Figure) -['data', 'frames', 'layout']
plotly.offline.iplot(plotly_fig, filename='test_MatPlotLib_modif_Plotly')

谢谢你,

【问题讨论】:

    标签: python matplotlib plotly


    【解决方案1】:

    简而言之,plotly 的 mpl_to_plotly 目前(截至 2017 年 8 月)无法转换饼图。

    更长的版本:matplotlib 饼图由matplotlib.patches.Wedge 对象组成。那些是matplotlib.patches.Patches。如果mpl_to_plotly 找到一个补丁,它将把它转换为一个路径并假设它是条形图的一部分。由于它不是在这种情况下,它会放弃,抛出一个警告“我发现了一个路径对象,我认为它不是条形图的一部分。忽略。”。因此,生成的图形将没有任何数据,并且会引发问题中的错误。

    【讨论】:

    • 有没有相关的问题可以解决这个问题。 Plotly 维护者是否承认这个错误?我们能帮忙吗?如果有问题可以进一步跟进,那就太好了
    • @JustInTime 首先,这不是一个真正的错误,而是一个未实现的功能。鉴于代码抛出警告表明开发人员确实意识到此功能缺失。由于 plotly 不像 matplotlib 等其他真正的开源项目那样由社区维护,因此您不能向源提供拉取请求等。但您当然可以在 community.plot.ly 上提及它,看看人们对此有何评论。
    • 感谢您的回复,如果我在这里误用了 Bug 这个词,请见谅。我会在 plot.ly 社区中提到它,看看我是否可以提供帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2015-01-06
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    相关资源
    最近更新 更多