Reputation: 45
I am trying to plot a line graph of the yield spread from 1993 to 2020 using plotly. The name of the variable is "yieldsp" in the dataframe called "data". It is a time-series data with a DateTime index as follows:
data['yieldsp'].head()
Date
1993-10-01 2.36
1993-10-04 2.32
1993-10-05 2.29
1993-10-06 2.31
1993-10-07 2.28
Name: yieldsp, dtype: float64
data.index
DatetimeIndex(['1993-10-01', '1993-10-04', '1993-10-05', '1993-10-06',
'1993-10-07', '1993-10-08', '1993-10-12', '1993-10-13',
'1993-10-14', '1993-10-15',
...
'2020-06-12', '2020-06-15', '2020-06-16', '2020-06-17',
'2020-06-18', '2020-06-19', '2020-06-22', '2020-06-23',
'2020-06-24', '2020-06-25'],
dtype='datetime64[ns]', name='Date', length=6688, freq=None)
I wrote the following code to get the plot:
# Using plotly.express
import plotly.express as px
#data = px.data.iris()
fig = px.line(data['yieldsp'], x = data.index, y ='Yield Spread', line_shape="spline", render_mode="svg")
fig.show()
But it generated the following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-49-92ef77a6fd5a> in <module>
5
6 fig = px.line(data['yieldsp'], x = data.index, y ='Yield Spread', color="continent", line_shape="spline",
----> 7 render_mode="svg")
8 fig.show()
~/opt/anaconda3/envs/tsa_course/lib/python3.7/site-packages/plotly/express/_chart_types.py in line(data_frame, x, y, line_group, color, line_dash, hover_name, hover_data, custom_data, text, facet_row, facet_col, facet_col_wrap, error_x, error_x_minus, error_y, error_y_minus, animation_frame, animation_group, category_orders, labels, orientation, color_discrete_sequence, color_discrete_map, line_dash_sequence, line_dash_map, log_x, log_y, range_x, range_y, line_shape, render_mode, title, template, width, height)
242 a polyline mark in 2D space.
243 """
--> 244 return make_figure(args=locals(), constructor=go.Scatter)
245
246
~/opt/anaconda3/envs/tsa_course/lib/python3.7/site-packages/plotly/express/_core.py in make_figure(args, constructor, trace_patch, layout_patch)
1753 apply_default_cascade(args)
1754
-> 1755 args = build_dataframe(args, constructor)
1756 if constructor in [go.Treemap, go.Sunburst] and args["path"] is not None:
1757 args = process_dataframe_hierarchy(args)
~/opt/anaconda3/envs/tsa_course/lib/python3.7/site-packages/plotly/express/_core.py in build_dataframe(args, constructor)
1311
1312 df_output, wide_id_vars = process_args_into_dataframe(
-> 1313 args, wide_mode, var_name, value_name
1314 )
1315
~/opt/anaconda3/envs/tsa_course/lib/python3.7/site-packages/plotly/express/_core.py in process_args_into_dataframe(args, wide_mode, var_name, value_name)
1117 if argument == "index":
1118 err_msg += "\n To use the index, pass it in directly as `df.index`."
-> 1119 raise ValueError(err_msg)
1120 elif length and len(df_input[argument]) != length:
1121 raise ValueError(
ValueError: Value of 'y' is not the name of a column in 'data_frame'. Expected one of ['yieldsp'] but received: Yield Spread
Before posting this question, I also looked at the solution of a similar question posted in StackOverflow, but that was not using a DateTime index, and so I was unable to resolve the error.
Upvotes: 1
Views: 6486
Reputation: 61084
Your data sample and description of your data is incomplete. You're displaying your data as data['yieldsp']
, but judging by your attempt to run px.line
you've got other variables like continent
in your data
as well.
Anyway, what you're trying to de here is to run px.line
on a dataset of wide format. And that's possible with the very latest versions of px.express
. But what will not work is assigning a string to the y
method and expect that you'll name your line that way. y
is a method that takes data as an argument defined as a string as a reference to your dataset. Using go.Scatter()
you could use name='yieldsp
to rename your line. But that's not possible here. So the easiest thing here would be to rename your variable in your dataset before plotting. You still haven't provided a complete data sample, but here is how you can build your plot given that continent
is not in your dataset.
Plot:
Complete code:
import plotly.express as px
import pandas as pd
# data that hopefullt represents your real world dataset
data = pd.DataFrame({'Date': {0: '1993-10-01',
1: '1993-10-04',
2: '1993-10-05',
3: '1993-10-06',
4: '1993-10-07'},
'yieldspd': {0: 2.36, 1: 2.32, 2: 2.29, 3: 2.31, 4: 2.28}})
data.set_index('Date', inplace = True)
# rename 'yieldspd'
data = data.rename(columns={'yieldspd': 'Yield Spread'})
# produce figure
fig = px.line(data, x = data.index, y ='Yield Spread', line_shape="spline")
# show figure
fig.show()
Upvotes: 2