Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions plotnine/ggplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
if TYPE_CHECKING:
from typing import Protocol

import pandas as pd
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from typing_extensions import Self
Expand Down Expand Up @@ -771,6 +772,25 @@ def save(
with plot_context(self).rc_context:
sv.figure.savefig(**sv.kwargs)

def layer_data(self, i: int = 0) -> pd.DataFrame:
"""
Return the data used to draw a specific plot layer

Parameters
----------
i :
Index of the layer to retrieve, starting from 0.

Returns
-------
pd.DataFrame
Data used by the specified layer after all transformations,
statistics, and position adjustments have been applied.
"""
p = deepcopy(self)
p._build()
return p.layers.data[i]


ggsave = ggplot.save

Expand Down
21 changes: 0 additions & 21 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,27 +218,6 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
warnings.resetwarnings()


def layer_data(p, i=0):
"""
Return layer information used to draw the plot

Parameters
----------
p : ggplot
ggplot object
i : int
Layer number

Returns
-------
out : dataframe
Layer information
"""
p = deepcopy(p)
p._build()
return p.layers.data[i]


def composition_equals(cmp: Compose, name: str) -> bool:
"""
Compare plot composition to image determined by `right`
Expand Down
6 changes: 2 additions & 4 deletions tests/test_geom_bar_col_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
)
from plotnine.stats.binning import freedman_diaconis_bins

from .conftest import layer_data

n = 10 # Some even number greater than 2

# ladder: 0 1 times, 1 2 times, 2 3 times, ...
Expand Down Expand Up @@ -60,8 +58,8 @@ def test_histogram_count():
def test_scale_transformed_breaks():
data = pd.DataFrame({"x": np.repeat(range(1, 5), range(1, 5))})
p = ggplot(data, aes("x")) + geom_histogram(breaks=[1, 2.5, 4])
out1 = layer_data(p)
out2 = layer_data(p + scale_x_sqrt())
out1 = p.layer_data()
out2 = (p + scale_x_sqrt()).layer_data()
np.testing.assert_allclose(out1.xmin, [1, 2.5])
np.testing.assert_allclose(out2.xmin, np.sqrt([1, 2.5]))

Expand Down
6 changes: 2 additions & 4 deletions tests/test_geom_bin_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

from plotnine import aes, geom_bin_2d, ggplot, scale_x_log10

from .conftest import layer_data

n = 20 # Make even for best results
reps = np.hstack(
[np.arange(int(np.ceil(n / 2))), np.arange(int(np.ceil(n // 2)))[::-1]]
Expand Down Expand Up @@ -34,7 +32,7 @@ def test_scale_transformed_breaks():
p = ggplot(data, aes("x", "y")) + geom_bin_2d(
breaks=([5, 50, 500], [0.5, 1.5, 2.5])
)
out1 = layer_data(p)
out2 = layer_data(p + scale_x_log10())
out1 = p.layer_data()
out2 = (p + scale_x_log10()).layer_data()
np.testing.assert_allclose(out1.xmax, [50, 500])
np.testing.assert_allclose(out2.xmax, np.log10([50, 500]))