I have a Python list that I want to use to chart a line chart in Plotly. The list contains accidents in each week of the year. So for instance list[5]=12 would indicate that in week 5 of 2020, there were 12 accidents. I want to draw a line chart showing the trend of accidents in 2020.
The full list split into two lists is as follows:
x axis [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53]
y axis [0, 0, 0, 2, 3, 4, 2, 3, 2, 2, 6, 4, 2, 1, 2, 1, 1, 3, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 3, 0, 0, 0, 2, 0, 0, 3, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0]
If I enumerate the single list it is as follows:
for e,i in enumerate(my_list):
print(f"e->{e}, i->{i}")
e->0, i->0
e->1, i->0
e->2, i->0
e->3, i->2
e->4, i->3
e->5, i->4
e->6, i->2
e->7, i->3
e->8, i->2
e->9, i->2
e->10, i->6
e->11, i->4
e->12, i->2
e->13, i->1
e->14, i->2
e->15, i->1
e->16, i->1
e->17, i->3
e->18, i->1
e->19, i->0
e->20, i->0
e->21, i->0
e->22, i->0
e->23, i->0
e->24, i->0
e->25, i->0
e->26, i->0
e->27, i->0
e->28, i->0
e->29, i->0
e->30, i->0
e->31, i->0
e->32, i->0
e->33, i->0
e->34, i->0
e->35, i->0
e->36, i->0
e->37, i->0
e->38, i->0
e->39, i->0
e->40, i->0
e->41, i->0
e->42, i->0
e->43, i->0
e->44, i->0
e->45, i->0
e->46, i->0
e->47, i->0
e->48, i->0
e->49, i->0
e->50, i->0
e->51, i->0
e->52, i->0
e->53, i->0
The index of the list will be X axis and the value will be the Y axis. Normally what I do is use two lists as follows:
# load the x and y axis
my_figure = px.line(x=x_list, y=y_list, title="Testing")
# now draw the line chart
dcc.Graph(id='my_figure', figure=my_figure),
However, in this case I only have one list, and I want to avoid copying the index value into a 2nd list. How can I use the single list with Plotly to draw a line chart?
You can create a dataframe and then try plotting with that dataframe
df = pd.DataFrame({'x_data':x_list, 'y_data':y_list})
px.line(df, x='x_data', y='y_data', title="Testing")
px.line(df, y='y_data', title="Testing")
also works in this case