-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Makes it possible to avoid string output that follows the output image. 2. Gained the ability to control the interactive figure format from within plotnine. closes #694
- Loading branch information
Showing
9 changed files
with
264 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -548,6 +548,7 @@ quartodoc: | |
- current_theme | ||
- dpi | ||
- figure_size | ||
- figure_format | ||
- get_option | ||
- set_option | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import pandas as pd | ||
|
||
if TYPE_CHECKING: | ||
from typing_extensions import Self | ||
|
||
from plotnine import ggplot | ||
|
||
|
||
class plot_context: | ||
""" | ||
Context to setup the environment within with the plot is built | ||
Parameters | ||
---------- | ||
plot : | ||
ggplot object to be built within the context. | ||
exits. | ||
show : | ||
Whether to show the plot. | ||
""" | ||
|
||
def __init__(self, plot: ggplot, show: bool = False): | ||
self.plot = plot | ||
self.show = show | ||
|
||
def __enter__(self) -> Self: | ||
""" | ||
Enclose in matplolib & pandas environments | ||
""" | ||
import matplotlib as mpl | ||
|
||
self.plot.theme._targets = {} | ||
self.rc_context = mpl.rc_context(self.plot.theme.rcParams) | ||
|
||
# Pandas deprecated is_copy, and when we create new dataframes | ||
# from slices we do not want complaints. We always uses the | ||
# new frames knowing that they are separate from the original. | ||
self.pd_option_context = pd.option_context( | ||
"mode.chained_assignment", None | ||
) | ||
self.rc_context.__enter__() | ||
self.pd_option_context.__enter__() | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, exc_traceback): | ||
""" | ||
Exit matplotlib & pandas environments | ||
""" | ||
import matplotlib.pyplot as plt | ||
|
||
if exc_type is None: | ||
if self.show: | ||
plt.show() | ||
else: | ||
plt.close(self.plot.figure) | ||
else: | ||
# There is an exception, close any figure | ||
if hasattr(self.plot, "figure"): | ||
plt.close(self.plot.figure) | ||
|
||
self.rc_context.__exit__(exc_type, exc_value, exc_traceback) | ||
self.pd_option_context.__exit__(exc_type, exc_value, exc_traceback) | ||
delattr(self.plot.theme, "_targets") | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Callable, Literal, TypeAlias | ||
|
||
from IPython.core.interactiveshell import InteractiveShell | ||
|
||
FigureFormat: TypeAlias = Literal[ | ||
"png", "retina", "jpeg", "jpg", "svg", "pdf" | ||
] | ||
|
||
|
||
def get_ipython() -> "InteractiveShell": | ||
""" | ||
Return running IPython instance or None | ||
""" | ||
try: | ||
from IPython.core.getipython import get_ipython as _get_ipython | ||
except ImportError as err: | ||
raise type(err)("IPython is has not been installed.") from err | ||
|
||
ip = _get_ipython() | ||
if ip is None: | ||
raise RuntimeError("Not running in a juptyer session.") | ||
|
||
return ip | ||
|
||
|
||
def is_inline_backend(): | ||
""" | ||
Return True if the inline_backend is on | ||
This can only be True if also running in an jupyter/ipython session. | ||
""" | ||
import matplotlib as mpl | ||
|
||
return "matplotlib_inline.backend_inline" in mpl.get_backend() | ||
|
||
|
||
def get_display_function(format: FigureFormat) -> Callable[[bytes], None]: | ||
""" | ||
Return a function that will display the plot image | ||
""" | ||
from IPython.display import ( | ||
SVG, | ||
Image, | ||
display_jpeg, | ||
display_pdf, | ||
display_png, | ||
display_svg, | ||
) | ||
|
||
def png(b: bytes): | ||
display_png(Image(b, format="png")) | ||
|
||
def retina(b: bytes): | ||
display_png(Image(b, format="png", retina=True)) | ||
|
||
def jpeg(b: bytes): | ||
display_jpeg(Image(b, format="jpeg")) | ||
|
||
def svg(b: bytes): | ||
display_svg(SVG(b)) | ||
|
||
def pdf(b: bytes): | ||
display_pdf(b, raw=True) | ||
|
||
lookup = { | ||
"png": png, | ||
"retina": retina, | ||
"jpeg": jpeg, | ||
"jpg": jpeg, | ||
"svg": svg, | ||
"pdf": pdf, | ||
} | ||
return lookup[format] | ||
Oops, something went wrong.