Home > Enterprise >  AttributeError: module 'matplotlib.dates' has no attribute '_SwitchableDateConverter&
AttributeError: module 'matplotlib.dates' has no attribute '_SwitchableDateConverter&

Time:11-29

Trying to produce a time-series choropleth of US election results. In trying to add a popup button on each state :

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

I run in the following error:

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:\anaconda3\lib\site-packages\mpld3\_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:\anaconda3\lib\site-packages\mpld3\mplexporter\exporter.py in run(self, fig)
     49             import matplotlib.pyplot as plt
     50             plt.close(fig)
---> 51         self.crawl_fig(fig)
     52 
     53     @staticmethod

D:\anaconda3\lib\site-packages\mpld3\mplexporter\exporter.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:\anaconda3\lib\site-packages\mpld3\mplexporter\exporter.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:\anaconda3\lib\site-packages\mpld3\mplexporter\utils.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'

I am running a Jupyter notebook on windows and cannot find anything about '_SwitchableDateConverter'.

For completion the getFigure function is as follow:

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

Thank you for your help

CodePudding user response:

Same issue here while using mpld3 latest version. I tried downgrading to matplotlib 3.2.1 and mpld3 0.5.5 and it works.

  • Related