不接受日期时间的 plotly (px) animation_frame 错误

标签 plotly plotly-express

我想通过 plotly 制作一个类似于以下示例的动画条形图:https://plotly.com/python/animations/

我有以下代码:

fig = px.bar(
  eu_vaccine_df.sort_values('date'), 
  x='country', y='people_vaccinated_per_hundred',
  color='country',
  animation_frame='date',
  animation_group='country',
  hover_name='country',
  range_y=[0,50],
  range_x=[0,30]
)
fig.update_layout(
  template='plotly_dark',
  margin=dict(r=10, t=25, b=40, l=60)
)
fig.show()

我认为问题与我的数据框的“日期”列有关。目前它是一个日期时间,并已使用 pd.to_datetime(covid_df['date']

我得到的错误如下:

ValueError                                Traceback (most recent call last)
<ipython-input-185-ff9c8cc72d87> in <module>
      7   hover_name='country',
      8   range_y=[0,50],
----> 9   range_x=[0,30]

     10 )
     11 fig.update_layout(

/opt/conda/lib/python3.7/site-packages/plotly/express/_chart_types.py in bar(data_frame, x, y, color, facet_row, facet_col, facet_col_wrap, facet_row_spacing, facet_col_spacing, hover_name, hover_data, custom_data, text, base, error_x, error_x_minus, error_y, error_y_minus, animation_frame, animation_group, category_orders, labels, color_discrete_sequence, color_discrete_map, color_continuous_scale, range_color, color_continuous_midpoint, opacity, orientation, barmode, log_x, log_y, range_x, range_y, title, template, width, height)
    352         constructor=go.Bar,
    353         trace_patch=dict(textposition="auto"),
--> 354         layout_patch=dict(barmode=barmode),
    355     )
    356 

/opt/conda/lib/python3.7/site-packages/plotly/express/_core.py in make_figure(args, constructor, trace_patch, layout_patch)
   2087     if "template" in args and args["template"] is not None:
   2088         fig.update_layout(template=args["template"], overwrite=True)
-> 2089     fig.frames = frame_list if len(frames) > 1 else []
   2090 
   2091     fig._px_trendlines = pd.DataFrame(trendline_rows)

/opt/conda/lib/python3.7/site-packages/plotly/basedatatypes.py in __setattr__(self, prop, value)
    719         if prop.startswith("_") or hasattr(self, prop):
    720             # Let known properties and private properties through
--> 721             super(BaseFigure, self).__setattr__(prop, value)
    722         else:
    723             # Raise error on unknown public properties

/opt/conda/lib/python3.7/site-packages/plotly/basedatatypes.py in frames(self, new_frames)
   2853 
   2854         # Validate frames
-> 2855         self._frame_objs = self._frames_validator.validate_coerce(new_frames)
   2856 
   2857     # Update

/opt/conda/lib/python3.7/site-packages/_plotly_utils/basevalidators.py in validate_coerce(self, v, skip_invalid)
   2540                     res.append(self.data_class(v_el))
   2541                 elif isinstance(v_el, dict):
-> 2542                     res.append(self.data_class(v_el, skip_invalid=skip_invalid))
   2543                 else:
   2544                     if skip_invalid:

/opt/conda/lib/python3.7/site-packages/plotly/graph_objs/_frame.py in __init__(self, arg, baseframe, data, group, layout, name, traces, **kwargs)
    253         _v = name if name is not None else _v
    254         if _v is not None:
--> 255             self["name"] = _v
    256         _v = arg.pop("traces", None)
    257         _v = traces if traces is not None else _v

/opt/conda/lib/python3.7/site-packages/plotly/basedatatypes.py in __setitem__(self, prop, value)
   4802                 # ### Handle simple property ###
   4803                 else:
-> 4804                     self._set_prop(prop, value)
   4805             else:
   4806                 # Make sure properties dict is initialized

/opt/conda/lib/python3.7/site-packages/plotly/basedatatypes.py in _set_prop(self, prop, val)
   5146                 return
   5147             else:
-> 5148                 raise err
   5149 
   5150         # val is None

/opt/conda/lib/python3.7/site-packages/plotly/basedatatypes.py in _set_prop(self, prop, val)
   5141 
   5142         try:
-> 5143             val = validator.validate_coerce(val)
   5144         except ValueError as err:
   5145             if self._skip_invalid:

/opt/conda/lib/python3.7/site-packages/_plotly_utils/basevalidators.py in validate_coerce(self, v)
   1084                     v = str(v)
   1085                 else:
-> 1086                     self.raise_invalid_val(v)
   1087 
   1088             if self.no_blank and len(v) == 0:

/opt/conda/lib/python3.7/site-packages/_plotly_utils/basevalidators.py in raise_invalid_val(self, v, inds)
    285                 typ=type_str(v),
    286                 v=repr(v),
--> 287                 valid_clr_desc=self.description(),
    288             )
    289         )

ValueError: 
     Invalid value of type 'pandas._libs.tslibs.timestamps.Timestamp' received for the 'name' property of frame
            Received value: Timestamp('2020-12-13 00:00:00')
    
        The 'name' property is a string and must be specified as:
          - A string
          - A number that will be converted to a string

最佳答案

您应该检查您的“日期”列的类型(更多信息在本文底部)。例如,以下代码作为一个整体工作。

import plotly.express as px
import pandas as pd

url = 'https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations.csv'
df = pd.read_csv(url)

country = 'location'
fig = px.bar(
  df.sort_values('date'), 
  x=country, y='people_vaccinated_per_hundred',
  color=country,
  animation_frame='date',
  animation_group=country,
  hover_name=country,
  range_y=[0,50],
  range_x=[0,30]
)
fig.update_layout(
  template='plotly_dark',
  margin=dict(r=10, t=25, b=40, l=60)
)
fig.show()

plotly

不过,我假设您的数据集的“日期”列不是字符串。要检查它是字符串还是其他类型(对于上述数据集,此行已经计算为 str):

type(df['date'][0])

要创建将日期转换为字符串的列,请将此行放在 fig 赋值行之前:

df['date_str'] = df['date'].apply(lambda x: str(x))

然后,在无花果分配行中,需要将动画帧设置为新列的名称(例如 animation_frame='date_str')。

关于不接受日期时间的 plotly (px) animation_frame 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66789515/

相关文章:

javascript - 如何使用正确的值在 plotly.js 中制作堆积面积图?

python - Plotly:躲避散点图分类轴上的重叠点

python - 如何在 Python (plotly.express, px) 中使用 Plotly Express 仅显示图例的一个子集?

python - 如何返回由 2 个下拉列表更新的数据表作为 Plotly Python 中的输入

javascript - Plotly.js 中带有(计算的)平均线的堆积条形图

r - 如何在绘图中手动将线型分配给线条

R 绘制如何将文本链接到气泡以便在单击图例时将其删除

python - Plotly Express 散点调整点大小

python - plotly 表达 plotly 有褪色的颜色