问题描述
我想通过 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,----> 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,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,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,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,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()
不过,我假设您数据集的“日期”列不是字符串。要检查它是字符串还是另一种类型(对于上述数据集,此行已评估为 str
):
type(df['date'][0])
要创建一个将日期转换为字符串的列,请将此行放在 fig
赋值行之前:
df['date_str'] = df['date'].apply(lambda x: str(x))
然后,在 fig 分配行中,需要将动画帧设置为新列的名称(例如 animation_frame='date_str'
)。