将 matplotlib 饼图转换为绘图字典



当我尝试将 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')

谢谢

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

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

最新更新