绘图:使用绘图和日期时间索引绘制时间序列

时间:2020-06-29 12:04:32

标签: python datetime graph time-series plotly

我正在尝试使用plotly绘制1993年至2020年收益率传播的折线图。在称为“数据”的数据框中,变量的名称为“ yieldsp”。它是具有DateTime索引的时间序列数据,如下所示:

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)

我编写了以下代码来获取图:

# 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()

但是它产生了以下错误:

---------------------------------------------------------------------------
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

在发布此问题之前,我还查看了StackOverflow中发布的similar question的解决方案,但是该解决方案未使用DateTime索引,因此无法解决该错误。

1 个答案:

答案 0 :(得分:1)

您的数据样本和数据描述不完整。您将数据显示为data['yieldsp'],但是从您尝试运行px.line的角度来看,continent中也有其他变量,例如data

无论如何,您要在此处进行的操作是在宽格式的数据集上运行px.line。那就是px.express中的possible with the very latest versions。但是起作用的是为y方法分配一个字符串,并希望您以这种方式 name 行。 y是一种以 data 作为参数的方法,该参数定义为字符串,作为对数据集的引用。使用go.Scatter(),您可以使用name='yieldsp重命名行。但这在这里是不可能的。因此,最简单的方法是在绘制之前在数据集中重命名变量。您仍然没有提供完整的数据样本,但是如果数据集中的continent不是 ,则可以按照以下方法构建图。

情节:

enter image description here

完整代码:

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()