将matplotlib饼图转换为图解字典

时间:2017-08-23 06:49:24

标签: python matplotlib plotly

当我尝试将MatPlotLib生成的饼图转换为Plotly字典时,我收到以下错误: PlotlyEmptyDataError:找到空数据列表。确保填充了要发送的数据对象列表,然后重试。

的确,如果我执行 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')

谢谢,

1 个答案:

答案 0 :(得分:2)

为了简短起见,情节mpl_to_plotly目前(截至2017年8月)无法转换饼图。

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