@@ -103,8 +103,16 @@ def __init__(self, canvas, *args, **kwargs):
103
103
def export (self ):
104
104
buf = io .BytesIO ()
105
105
self .canvas .figure .savefig (buf , format = 'png' , dpi = 'figure' )
106
- data = "<img src='data:image/png;base64,{0}'/>"
107
- data = data .format (b64encode (buf .getvalue ()).decode ('utf-8' ))
106
+ # Figure width in pixels
107
+ pwidth = (self .canvas .figure .get_figwidth () *
108
+ self .canvas .figure .get_dpi ())
109
+ # Scale size to match widget on HiDPI monitors.
110
+ if hasattr (self .canvas , 'device_pixel_ratio' ): # Matplotlib 3.5+
111
+ width = pwidth / self .canvas .device_pixel_ratio
112
+ else :
113
+ width = pwidth / self .canvas ._dpi_ratio
114
+ data = "<img src='data:image/png;base64,{0}' width={1}/>"
115
+ data = data .format (b64encode (buf .getvalue ()).decode ('utf-8' ), width )
108
116
display (HTML (data ))
109
117
110
118
@default ('toolitems' )
@@ -283,10 +291,26 @@ def _repr_mimebundle_(self, **kwargs):
283
291
buf = io .BytesIO ()
284
292
self .figure .savefig (buf , format = 'png' , dpi = 'figure' )
285
293
self ._data_url = b64encode (buf .getvalue ()).decode ('utf-8' )
294
+ # Figure width in pixels
295
+ pwidth = (self .figure .get_figwidth () *
296
+ self .figure .get_dpi ())
297
+ # Scale size to match widget on HiDPI monitors.
298
+ if hasattr (self , 'device_pixel_ratio' ): # Matplotlib 3.5+
299
+ width = pwidth / self .device_pixel_ratio
300
+ else :
301
+ width = pwidth / self ._dpi_ratio
302
+ html = """
303
+ <div style="display: inline-block;">
304
+ <div class="jupyter-widgets widget-label" style="text-align: center;">{0}</div>
305
+ <img src='data:image/png;base64,{1}' width={2}/>
306
+ </div>
307
+ """ .format (
308
+ self ._figure_label , self ._data_url , width
309
+ )
286
310
287
311
data = {
288
312
'text/plain' : plaintext ,
289
- 'image/png ' : self . _data_url ,
313
+ 'text/html ' : html ,
290
314
'application/vnd.jupyter.widget-view+json' : {
291
315
'version_major' : 2 ,
292
316
'version_minor' : 0 ,
0 commit comments