Skip to content

Commit a449bf0

Browse files
committed
Add just parameter to geom_bar and geom_col
1 parent dd5a537 commit a449bf0

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

doc/changelog.qmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ title: Changelog
6868
)
6969
```
7070

71+
- Geoms [](:class:`~plotnine.geom_bar`) and [](:class:`~plotnine.geom_col`) have
72+
gained new parameter `just` that controls how the bars align with the axis break point.
73+
7174
### Enhancements
7275

7376
- Included datasets [](:attr:`~plotnine.data.mpg`), [](:attr:`~plotnine.data.msleep`) and

plotnine/geoms/geom_bar.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class geom_bar(geom_rect):
2020
Parameters
2121
----------
2222
{common_parameters}
23+
just : float, default=0.5
24+
How to align the column with respect to the axis breaks. The default
25+
`0.5` aligns the center of the column with the break. `0` aligns the
26+
left of the of the column with the break and `1` aligns the right of
27+
the column with the break.
2328
width : float, default=None
2429
Bar width. If `None`{.py}, the width is set to
2530
`90%` of the resolution of the data.
@@ -35,6 +40,7 @@ class geom_bar(geom_rect):
3540
"stat": "count",
3641
"position": "stack",
3742
"na_rm": False,
43+
"just": 0.5,
3844
"width": None,
3945
}
4046

@@ -45,6 +51,8 @@ def setup_data(self, data: pd.DataFrame) -> pd.DataFrame:
4551
else:
4652
data["width"] = resolution(data["x"], False) * 0.9
4753

54+
just = self.params.get("just", 0.5)
55+
4856
bool_idx = data["y"] < 0
4957

5058
data["ymin"] = 0.0
@@ -53,7 +61,7 @@ def setup_data(self, data: pd.DataFrame) -> pd.DataFrame:
5361
data["ymax"] = data["y"]
5462
data.loc[bool_idx, "ymax"] = 0.0
5563

56-
data["xmin"] = data["x"] - data["width"] / 2
57-
data["xmax"] = data["x"] + data["width"] / 2
64+
data["xmin"] = data["x"] - data["width"] * just
65+
data["xmax"] = data["x"] + data["width"] * (1 - just)
5866
del data["width"]
5967
return data

plotnine/geoms/geom_col.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class geom_col(geom_bar):
1717
Parameters
1818
----------
1919
{common_parameters}
20+
just : float, default=0.5
21+
How to align the column with respect to the axis breaks. The default
22+
`0.5` aligns the center of the column with the break. `0` aligns the
23+
left of the of the column with the break and `1` aligns the right of
24+
the column with the break.
2025
width : float, default=None
2126
Bar width. If `None`{.py}, the width is set to
2227
`90%` of the resolution of the data.
@@ -32,5 +37,6 @@ class geom_col(geom_bar):
3237
"stat": "identity",
3338
"position": "stack",
3439
"na_rm": False,
40+
"just": 0.5,
3541
"width": None,
3642
}
Loading

tests/test_geom_bar_col_histogram.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ def test_col():
4040
assert p == "col"
4141

4242

43+
def test_col_just():
44+
data = pd.DataFrame({"x": range(1, 4), "y": range(1, 4)})
45+
p = (
46+
ggplot(data, aes("x", "y"))
47+
+ geom_col(just=0, fill="red", width=1 / 3) # left
48+
+ geom_col(just=1, fill="blue", width=1 / 3) # right
49+
+ geom_col(just=0.5, fill="green", width=1 / 3, alpha=0.7) # center
50+
)
51+
assert p == "col_just"
52+
53+
4354
def test_histogram_count():
4455
p = ggplot(data, aes("x")) + geom_histogram(aes(fill="factor(z)"), bins=n)
4556

0 commit comments

Comments
 (0)