Home > Net >  Plotly graph is displaying as empty
Plotly graph is displaying as empty

Time:05-17

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

CodePudding user response:

Ì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

  • Related