@@ -175,7 +175,23 @@ def __init__(self, url, download=False):
175175 ]
176176
177177class Figure (Element ):
178- def __init__ (self , figsize = (17 ,10 )):
178+ def __init__ (self , width = "100%" , height = None , ratio = "60%" , figsize = None ):
179+ """Create a Figure object, to plot things into it.
180+
181+ Parameters
182+ ----------
183+ width : str, default "100%"
184+ The width of the Figure. It may be a percentage or a distance (like "300px").
185+ height : str, default None
186+ The height of the Figure. It may be a percentage or a distance (like "300px").
187+ ratio : str, default "60%"
188+ A percentage defining the aspect ratio of the Figure.
189+ It will be ignored if height is not None.
190+ figsize : tuple of two int, default None
191+ If you're a matplotlib addict, you can overwrite width and height.
192+ Values will be converted into pixels in using 60 dpi.
193+ For example figsize=(10,5) wil result in width="600px", height="300px".
194+ """
179195 super (Figure , self ).__init__ ()
180196 self ._name = 'Figure'
181197 self .header = Element ()
@@ -186,7 +202,12 @@ def __init__(self, figsize=(17,10)):
186202 self .html ._parent = self
187203 self .script ._parent = self
188204
189- self .figsize = figsize
205+ self .width = width
206+ self .height = height
207+ self .ratio = ratio
208+ if figsize is not None :
209+ self .width = str (60 * figsize [0 ])+ 'px'
210+ self .height = str (60 * figsize [1 ])+ 'px'
190211
191212 self ._template = Template (u"""
192213 <!DOCTYPE html>
@@ -252,25 +273,27 @@ def _repr_html_(self, **kwargs):
252273
253274 Parameters
254275 ----------
255- self : folium.Map object
256- The map you want to display
257-
258- figsize : tuple of length 2, default (17,10)
259- The size of the output you expect in inches.
260- Output is 60dpi so that the output has same size as a
261- matplotlib figure with the same figsize.
262-
263276 """
264277 html = self .render (** kwargs )
265-
266- width , height = self .figsize
267-
268- iframe = '<iframe src="{html}" width="{width}px" height="{height}px"></iframe>' \
278+ html = "data:text/html;base64," + base64 .b64encode (html .encode ('utf8' )).decode ('utf8' )
279+
280+ if self .height is None :
281+ iframe = """
282+ <div style="width:{width};">
283+ <div style="position:relative;width:100%;height:0;padding-bottom:{ratio};">
284+ <iframe src="{html}" style="position:absolute;width:100%;height:100%;left:0;top:0;">
285+ </iframe>
286+ </div></div>""" .format (
287+ html = html ,
288+ width = self .width ,
289+ ratio = self .ratio ,
290+ )
291+ else :
292+ iframe = '<iframe src="{html}" width="{width}" height="{height}"></iframe>' \
269293 .format (\
270- html = "data:text/html;base64," + base64 .b64encode (html .encode ('utf8' )).decode ('utf8' ),
271- #html = self.HTML.replace('"','"'),
272- width = int (60. * width ),
273- height = int (60. * height ),
294+ html = html ,
295+ width = self .width ,
296+ height = self .height ,
274297 )
275298 return iframe
276299
@@ -387,26 +410,15 @@ def render(self, **kwargs):
387410 figure .script .add_children (Element (script (self , kwargs )),
388411 name = self .get_name ())
389412
390- def _repr_html_ (self , figsize = (17 ,10 ), ** kwargs ):
391- """Displays the Map in a Jupyter notebook.
392-
393- Parameters
394- ----------
395- self : folium.Map object
396- The map you want to display
397-
398- figsize : tuple of length 2, default (17,10)
399- The size of the output you expect in inches.
400- Output is 60dpi so that the output has same size as a
401- matplotlib figure with the same figsize.
402-
413+ def _repr_html_ (self , ** kwargs ):
414+ """Displays the Div in a Jupyter notebook.
403415 """
404416 if self ._parent is None :
405417 self .add_to (Figure ())
406- out = self ._parent ._repr_html_ (figsize = figsize , ** kwargs )
418+ out = self ._parent ._repr_html_ (** kwargs )
407419 self ._parent = None
408420 else :
409- out = self ._parent ._repr_html_ (figsize = figsize , ** kwargs )
421+ out = self ._parent ._repr_html_ (** kwargs )
410422 return out
411423
412424class MacroElement (Element ):
0 commit comments