0

I have a dataframe with the columns

 Car | Week | No_Users

where data about number of users per certain cars in several weeks is shown.

I made a pivot table

pivot = df.pivot_table(index='Car', columns='Week', values='No_Users', fill_value=0)

and i'm trying to build a plot with the pivot table in plotly

data = [go.Scatter(x=pivot.columns, y=pivot[pivot.index==name].values, mode='lines', name=name) for name in pivot.index]
pyo.plot(data)

I get no errors but the graph shows no lines. The x-axis and the line names are correct but the values aren't displayed in the graph. It's just empty

2
  • What is go in go.Scatter... ? Commented May 16, 2022 at 18:27
  • Oh i'm sorry, it's plotly.graph_objects Commented May 16, 2022 at 18:29

1 Answer 1

1

Ìn pivot[pivot.index==name].values, you got a list inside another list.

Just unpack it

Like this:

fig = go.Figure()

for name in pivot.index:
  fig.add_trace(
      go.Scatter( name=name, x=pivot.columns, y=(pivot[pivot.index==name].values)[0], mode='lines')
  )

fig.show()

if want see some clearer, check that colab link https://colab.research.google.com/drive/1vgbfEx4fd2P-2w4bTfjWv1Y76KQxwPow?usp=sharing

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.