@@ -401,89 +401,96 @@ def savefig( # noqa: PLR0912
401
401
if show :
402
402
launch_external_viewer (str (fname ))
403
403
404
- def show (self , dpi = 300 , width = 500 , method = None , waiting = 0.5 , ** kwargs ):
404
+ def show (
405
+ self ,
406
+ method : Literal ["external" , "notebook" , "none" , None ] = None ,
407
+ dpi : int = 300 ,
408
+ width : int = 500 ,
409
+ waiting : float = 0.5 ,
410
+ ** kwargs ,
411
+ ):
405
412
"""
406
413
Display a preview of the figure.
407
414
408
- Inserts the preview in the Jupyter notebook output if available,
409
- otherwise opens it in the default viewer for your operating system
410
- (falls back to the default web browser).
415
+ Inserts the preview in the Jupyter notebook output if available, otherwise opens
416
+ it in the default viewer for your operating system (falls back to the default
417
+ web browser).
411
418
412
- :func:`pygmt.set_display` can select the default display method
419
+ Use :func:`pygmt.set_display` to select the default display method
413
420
(``"notebook"``, ``"external"``, ``"none"`` or ``None``).
414
421
415
- The ``method`` parameter can also override the default display method
416
- for the current figure. Parameters ``dpi`` and ``width`` can be used
417
- to control the resolution and dimension of the figure in the notebook.
422
+ The ``method`` parameter allows to override the default display method for the
423
+ current figure. The parameters ``dpi`` and ``width`` can be used to control the
424
+ resolution and dimension of the figure in the notebook.
418
425
419
- **Note**: The external viewer can be disabled by setting the
420
- PYGMT_USE_EXTERNAL_DISPLAY environment variable to **false**.
421
- This is useful when running unit tests and building the documentation
422
- in consoles without a Graphical User Interface.
426
+ The external viewer can be disabled by setting the environment variable
427
+ **PYGMT_USE_EXTERNAL_DISPLAY** to ``"false"``. This is useful when running tests
428
+ and building the documentation to avoid popping up windows.
423
429
424
- Note that the external viewer does not block the current process, thus
425
- it's necessary to suspend the execution of the current process for a
426
- short while after launching the external viewer, so that the preview
427
- image won't be deleted before the external viewer tries to open it. Set
428
- the ``waiting`` parameter to a larger number if your computer is slow.
430
+ The external viewer does not block the current process, thus it's necessary to
431
+ suspend the execution of the current process for a short while after launching
432
+ the external viewer, so that the preview image won't be deleted before the
433
+ external viewer tries to open it. Set the ``waiting`` parameter to a larger
434
+ number if the image viewer on your computer is slow to open the figure .
429
435
430
436
Parameters
431
437
----------
432
- dpi : int
433
- The image resolution (dots per inch) in Jupyter notebooks.
434
- width : int
435
- The image width (in pixels) in Jupyter notebooks.
436
- method : str or None
437
- How the current figure will be displayed. Choose from:
438
+ method
439
+ The method to display the current image preview. Choose from:
438
440
439
441
- ``"external"``: External PDF preview using the default PDF viewer
440
442
- ``"notebook"``: Inline PNG preview in the current notebook
441
443
- ``"none"``: Disable image preview
442
444
- ``None``: Reset to the default display method
443
445
444
- The default display method is ``"external"`` in Python consoles or
446
+ The default display method is ``"external"`` in Python consoles and
445
447
``"notebook"`` in Jupyter notebooks, but can be changed by
446
448
:func:`pygmt.set_display`.
447
- waiting : float
448
- Suspend the execution of the current process for a given number of
449
- seconds after launching an external viewer.
450
- Only works if ``method="external"``.
449
+
450
+ dpi
451
+ The image resolution (dots per inch) in Jupyter notebooks.
452
+ width
453
+ The image width (in pixels) in Jupyter notebooks.
454
+ waiting
455
+ Suspend the execution of the current process for a given number of seconds
456
+ after launching an external viewer. Only works if ``method="external"``.
451
457
**kwargs : dict
452
- Additional keyword arguments passed to
453
- :meth:`pygmt.Figure.psconvert`. Valid parameters are ``gs_path``,
454
- ``gs_option``, ``resize``, ``bb_style``, and `` verbose``.
458
+ Additional keyword arguments passed to :meth:`pygmt.Figure.psconvert`. Valid
459
+ parameters are ``gs_path``, ``gs_option``, ``resize``, ``bb_style``, and
460
+ ``verbose``.
455
461
"""
456
- # Module level variable to know which figures had their show method
457
- # called. Needed for the sphinx-gallery scraper.
462
+ # Module level variable to know which figures had their show method called.
463
+ # Needed for the sphinx-gallery scraper.
458
464
SHOWED_FIGURES .append (self )
459
465
460
466
# Set the display method
461
467
if method is None :
462
468
method = SHOW_CONFIG ["method" ]
463
469
464
- if method not in {"external" , "notebook" , "none" }:
465
- raise GMTInvalidInput (
466
- f"Invalid display method '{ method } ', "
467
- "should be either 'notebook', 'external', or 'none'."
468
- )
469
-
470
- if method == "notebook" :
471
- if not _HAS_IPYTHON :
472
- raise GMTError (
473
- "Notebook display is selected, but IPython is not available. "
474
- "Make sure you have IPython installed, "
475
- "or run the script in a Jupyter notebook."
470
+ match method :
471
+ case "notebook" :
472
+ if not _HAS_IPYTHON :
473
+ raise GMTError (
474
+ "Notebook display is selected, but IPython is not available. "
475
+ "Make sure you have IPython installed, "
476
+ "or run the script in a Jupyter notebook."
477
+ )
478
+ png = self ._preview (
479
+ fmt = "png" , dpi = dpi , anti_alias = True , as_bytes = True , ** kwargs
480
+ )
481
+ IPython .display .display (IPython .display .Image (data = png , width = width ))
482
+ case "external" :
483
+ pdf = self ._preview (
484
+ fmt = "pdf" , dpi = dpi , anti_alias = False , as_bytes = False , ** kwargs
485
+ )
486
+ launch_external_viewer (pdf , waiting = waiting )
487
+ case "none" :
488
+ pass # Do nothing
489
+ case _:
490
+ raise GMTInvalidInput (
491
+ f"Invalid display method '{ method } '. Valid values are 'external', "
492
+ "'notebook', 'none' or None."
476
493
)
477
- png = self ._preview (
478
- fmt = "png" , dpi = dpi , anti_alias = True , as_bytes = True , ** kwargs
479
- )
480
- IPython .display .display (IPython .display .Image (data = png , width = width ))
481
-
482
- if method == "external" :
483
- pdf = self ._preview (
484
- fmt = "pdf" , dpi = dpi , anti_alias = False , as_bytes = False , ** kwargs
485
- )
486
- launch_external_viewer (pdf , waiting = waiting )
487
494
488
495
def _preview (self , fmt , dpi , as_bytes = False , ** kwargs ):
489
496
"""
0 commit comments