@@ -244,14 +244,17 @@ def _prepare_params_plot(
244244 # handle axes and size
245245 wspace = 0.75 / rcParams ["figure.figsize" ][0 ] + 0.02 if wspace is None else wspace
246246 figsize = rcParams ["figure.figsize" ] if figsize is None else figsize
247- dpi = rcParams ["figure.dpi" ] if dpi is None else dpi
247+ # When creating a new figure, fall back to rcParams; when the user provides
248+ # their own axes, preserve the figure's existing DPI (only override if
249+ # the user explicitly passed dpi= to show()).
250+ resolved_dpi = rcParams ["figure.dpi" ] if dpi is None else dpi
248251 if num_panels > 1 and ax is None :
249252 fig , grid = _panel_grid (
250253 num_panels = num_panels ,
251254 hspace = hspace ,
252255 wspace = wspace ,
253256 ncols = ncols ,
254- dpi = dpi ,
257+ dpi = resolved_dpi ,
255258 figsize = figsize ,
256259 )
257260 axs : None | Sequence [Axes ] = [plt .subplot (grid [c ]) for c in range (num_panels )]
@@ -266,14 +269,16 @@ def _prepare_params_plot(
266269 )
267270 assert ax is None or isinstance (ax , Sequence ), f"Invalid type of `ax`: { type (ax )} , expected `Sequence`."
268271 axs = ax
272+ if dpi is not None :
273+ fig .set_dpi (dpi )
269274 else :
270275 axs = None
271276 if ax is None :
272- fig , ax = plt .subplots (figsize = figsize , dpi = dpi , constrained_layout = True )
277+ fig , ax = plt .subplots (figsize = figsize , dpi = resolved_dpi , constrained_layout = True )
273278 elif isinstance (ax , Axes ):
274- # needed for rasterization if user provides Axes object
275279 fig = ax .get_figure ()
276- fig .set_dpi (dpi )
280+ if dpi is not None :
281+ fig .set_dpi (dpi )
277282
278283 # set scalebar
279284 if scalebar_dx is not None :
@@ -1955,10 +1960,10 @@ def _rasterize_if_necessary(
19551960 target_y_dims = dpi * height
19561961 target_x_dims = dpi * width
19571962
1958- # Heuristics for when to rasterize
1963+ # Rasterize when the source image is substantially larger than what the
1964+ # current figure DPI × size requires. The +100 margin avoids rasterizing
1965+ # when the image is only slightly larger than the target.
19591966 do_rasterization = y_dims > target_y_dims + 100 or x_dims > target_x_dims + 100
1960- if x_dims < 2000 and y_dims < 2000 :
1961- do_rasterization = False
19621967
19631968 if do_rasterization :
19641969 logger .info ("Rasterizing image for faster rendering." )
0 commit comments