I have a dataframe with the columns
JavaScript
x
2
1
Car | Week | No_Users
2
where data about number of users per certain cars in several weeks is shown.
I made a pivot table
JavaScript
1
2
1
pivot = df.pivot_table(index='Car', columns='Week', values='No_Users', fill_value=0)
2
and i’m trying to build a plot with the pivot table in plotly
JavaScript
1
3
1
data = [go.Scatter(x=pivot.columns, y=pivot[pivot.index==name].values, mode='lines', name=name) for name in pivot.index]
2
pyo.plot(data)
3
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
Advertisement
Answer
Ìn pivot[pivot.index==name].values
, you got a list inside another list.
Just unpack it
Like this:
JavaScript
1
9
1
fig = go.Figure()
2
3
for name in pivot.index:
4
fig.add_trace(
5
go.Scatter( name=name, x=pivot.columns, y=(pivot[pivot.index==name].values)[0], mode='lines')
6
)
7
8
fig.show()
9
if want see some clearer, check that colab link https://colab.research.google.com/drive/1vgbfEx4fd2P-2w4bTfjWv1Y76KQxwPow?usp=sharing