K.Ivi
K.Ivi

Reputation: 111

Convert a matplotlib pie chart to plotly dictionary

I am getting the following error, when I try to convert MatPlotLib generated pie chart to Plotly dictionary: PlotlyEmptyDataError: Empty data list found. Make sure that you populated the list of data objects you're sending and try again.

Indeed, if I execute plotly_fig (pls. see script bellow) I get data empty:

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

I couldn't find limitations for pie charts, so I am not sure if I do something wrong?

    #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')

Thank you,

Upvotes: 0

Views: 1429

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339210

To make it short, plotly's mpl_to_plotly is currently (as of August 2017) not able to convert pie charts.

The longer version: A matplotlib pie chart consists of matplotlib.patches.Wedge objects. Those are matplotlib.patches.Patches. If mpl_to_plotly finds a patch, it will convert it to a path and assume that it is part of a bar chart. Since it isn't in this case, it will give up, throwing a warning "I found a path object that I don't think is part of a bar chart. Ignoring.". The produced figure will therefore not have any data and the error as in the question is thrown.

Upvotes: 2

Related Questions