属性错误:模块 'matplotlib.dates' 没有属性 '_SwitchableDateConverter'



试图制作美国大选结果的时间序列合唱。在尝试在每个状态上添加一个弹出按钮时:

#plot choropleth button map
m = folium.Map(location=[50.77500, -100],zoom_start=3)
choropleth =folium.GeoJson(data= us_shape.to_json(),
style_function=style_function)
m.add_child(choropleth)
#Create popup button for each state
for i in range(len(us_shape)):

geometry = us_shape.loc[i]['geometry']
state_name = us_shape.loc[i]['state_name']
popup = folium.Popup(getFigure(state_name),max_width=1000)

state_marker = folium.GeoJson(data=mapping(geometry),
highlight_function = highlight_style)
state_marker.add_child(popup)
m.add_child(state_marker)
m.save("../figures/us_election_map2.html")

我在以下错误中运行:

AttributeError                            Traceback (most recent call last)
<ipython-input-271-f8158e5140b6> in <module>
12     state_name = us_shape.loc[i]['state_name']
13     print(state_name)
---> 14     popup = folium.Popup(getFigure(state_name),max_width=1000)
15     state_marker = folium.GeoJson(data=mapping(geometry),
16                                   highlight_function = highlight_style)
<ipython-input-249-50efd762b466> in getFigure(state)
37 
38     #Add figure to iframe
---> 39     html = mpld3.fig_to_html(fig)
40     iframe = folium.IFrame(html=html,width = 600, height = 300)
41 
D:anaconda3libsite-packagesmpld3_display.py in fig_to_html(fig, d3_url, mpld3_url, no_extras, template_type, figid, use_http, include_libraries, **kwargs)
248 
249     renderer = MPLD3Renderer()
--> 250     Exporter(renderer, close_mpl=False, **kwargs).run(fig)
251 
252     fig, figure_json, extra_css, extra_js = renderer.finished_figures[0]
D:anaconda3libsite-packagesmpld3mplexporterexporter.py in run(self, fig)
49             import matplotlib.pyplot as plt
50             plt.close(fig)
---> 51         self.crawl_fig(fig)
52 
53     @staticmethod
D:anaconda3libsite-packagesmpld3mplexporterexporter.py in crawl_fig(self, fig)
116                                        props=utils.get_figure_properties(fig)):
117             for ax in fig.axes:
--> 118                 self.crawl_ax(ax)
119 
120     def crawl_ax(self, ax):
D:anaconda3libsite-packagesmpld3mplexporterexporter.py in crawl_ax(self, ax)
121         """Crawl the axes and process all elements within"""
122         with self.renderer.draw_axes(ax=ax,
--> 123                                      props=utils.get_axes_properties(ax)):
124             for line in ax.lines:
125                 self.draw_line(ax, line)
D:anaconda3libsite-packagesmpld3mplexporterutils.py in get_axes_properties(ax)
296         lim = domain
297         if (
--> 298             isinstance(axis.converter, matplotlib.dates._SwitchableDateConverter) or
299             isinstance(axis.converter, matplotlib.dates.DateConverter) or
300             isinstance(axis.converter, matplotlib.dates.ConciseDateConverter)
AttributeError: module 'matplotlib.dates' has no attribute '_SwitchableDateConverter'

我正在windows上运行Jupyter笔记本,找不到有关"_SwitchableDateConverter"的任何信息。

为了完成,getFigure函数如下:

def getFigure(state):
"""
Plot voting trends from a given state
"""
#Get number of votes
years = range(1976,2020,4)
dems = []
reps =[]
for year in years:
result = results[year][state]
dems.append(result['dem']/1000000)  
reps.append(result['rep']/1000000) 
#Plot number of votes    
fig = plt.figure(figsize=(8,4))
plt.plot(years,dems,label='Democrat',color='#4f7bff')
plt.plot(years,reps,label='Republican',color='#ff5b4f')
plt.title(state,size = 18)
plt.ticklabel_format(style='plain')
plt.xlabel('Year',size =14)
plt.xticks(years)
plt.ylabel('Votes (millions)',size =14)
plt.legend(loc =0)
#Add figure to iframe
html = mpld3.fig_to_html(fig)
iframe = folium.IFrame(html=html,width = 600, height = 300)
return iframe

感谢您的帮助

在使用mpld3最新版本时也存在同样的问题。我尝试降级到matplotlib 3.2.1和mpld3 0.5.5,它很有效。

最新更新