1

I'm using Jupyter notebook that is drawing a bar chart in offline mode without issues.

I have a Pandas dataframe from reading in a CSV file and I want to quickly create a pie chart in Plotly (offline mode).

Plotly pie chart values (https://plot.ly/python/reference/#pie-values) counts the labels if omitted. That's what I had hoped for. I wanted to set the pie chart labels (https://plot.ly/python/reference/#pie-labels) directly from my pandas dataframe.

My code:

import plotly as py
import plotly.graph_objs as go
import pandas as pd
from plotly.offline import init_notebook_mode

init_notebook_mode()

df2 = pd.read_csv("./testdata.csv")

trace = go.Pie(
    labels= df2["Vendor"]
)

py.offline.plot(trace, filename='pdq_pie_chart.html')

I get an error when attempting this:

PlotlyError: The `figure_or_data` positional argument must be `dict`-like, `list`-like, or an instance of plotly.graph_objs.Figure

Did I miss something in the lead up to the go.Pie call? I deliberately didn't get into any formatting options, and wanted to generate the pie chart with as little code as possible from the dataframe.

2
  • 1
    Can you share test data and did you try labels= df2["Vendor"].values ? Commented Aug 21, 2018 at 4:04
  • hey @W.Dodge... I just tried that and no luck. I also just substituted simple arrays for labels and values, and still get the same error. Commented Aug 21, 2018 at 4:08

1 Answer 1

7

you required labels and values parameter to draw a pie chart in plotly. This example will use to draw a pie chart.

labels = df2['Vendor'].value_counts().index
values = df2['Vendor'].value_counts().values

trace = go.Pie(labels=labels, values=values)

py.iplot([trace], filename='basic_pie_chart')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.