Skip to content

fix: Alpha respected by polygon lines #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions plotnine/geoms/geom_boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def flat(*args):
'x': flat(data['x'], data['x']),
'y': flat(data['upper'], data['lower']),
'yend': flat(data['ymax'], data['ymin']),
'alpha': 1
'alpha': flat(data['alpha'], data['alpha'])
})
whiskers['xend'] = whiskers['x']
copy_missing_columns(whiskers, data[common_columns])
Expand Down Expand Up @@ -199,7 +199,9 @@ def draw_legend(data, da, lyr):
facecolor = to_rgba(data['fill'], data['alpha'])
if facecolor is None:
facecolor = 'none'

edgecolor = to_rgba(data['color'], data['alpha'])
if edgecolor is None:
edgecolor = 'none'
kwargs = dict(
linestyle=data['linetype'],
linewidth=data['size'])
Expand All @@ -208,14 +210,15 @@ def draw_legend(data, da, lyr):
width=da.width*.75,
height=da.height*.5,
facecolor=facecolor,
edgecolor=data['color'],
edgecolor=edgecolor,
capstyle='projecting',
antialiased=False,
**kwargs)
da.add_artist(box)

kwargs['solid_capstyle'] = 'butt'
kwargs['color'] = data['color']
kwargs['alpha'] = data['alpha']
kwargs['linewidth'] *= SIZE_FACTOR

# middle strike through
Expand Down
2 changes: 1 addition & 1 deletion plotnine/geoms/geom_crossbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def flat(*args):
'yend': y,
'group': group})
copy_missing_columns(middle, data)
middle['alpha'] = 1
middle['alpha'] = data['alpha']
middle['size'] *= params['fatten']

has_notch = ynotchlower is not None and ynotchupper is not None
Expand Down
8 changes: 6 additions & 2 deletions plotnine/geoms/geom_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def draw_group(data, panel_params, coord, ax, **params):
verts[i] = tuple(zip(df['x'], df['y']))
fill = to_rgba(df['fill'].iloc[0], df['alpha'].iloc[0])
facecolor[i] = 'none' if fill is None else fill
edgecolor[i] = df['color'].iloc[0] or 'none'
color = to_rgba(df['color'].iloc[0], df['alpha'].iloc[0])
edgecolor[i] = 'none' if color is None else color
linestyle[i] = df['linetype'].iloc[0]
linewidth[i] = df['size'].iloc[0]

Expand Down Expand Up @@ -96,6 +97,9 @@ def draw_legend(data, da, lyr):
da.width/4, da.height/4])
if data['color'] is None:
linewidth = 0
edgecolor = None
else:
edgecolor = to_rgba(data['color'], data['alpha'])

facecolor = to_rgba(data['fill'], data['alpha'])
if facecolor is None:
Expand All @@ -107,7 +111,7 @@ def draw_legend(data, da, lyr):
linewidth=linewidth,
linestyle=data['linetype'],
facecolor=facecolor,
edgecolor=data['color'],
edgecolor=edgecolor,
capstyle='projecting')
da.add_artist(rect)
return da
4 changes: 2 additions & 2 deletions plotnine/geoms/geom_rect.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ def draw_group(data, panel_params, coord, ax, **params):
verts[i] = [(l, b), (l, t), (r, t), (r, b)]

fill = to_rgba(data['fill'], data['alpha'])
color = data['color']

# prevent unnecessary borders
if all(color.isnull()):
if all(data['color'].isnull()):
color = 'none'
color = to_rgba(data['color'], data['alpha'])

col = PolyCollection(
verts,
Expand Down
6 changes: 3 additions & 3 deletions plotnine/geoms/geom_ribbon.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ def draw_group(data, panel_params, coord, ax, **params):
def draw_unit(data, panel_params, coord, ax, **params):
data['size'] *= SIZE_FACTOR
fill = to_rgba(data['fill'], data['alpha'])
color = data['color']

if fill is None:
fill = 'none'

if all(color.isnull()):
if all(data['color'].isnull()):
color = 'none'
else:
color = to_rgba(data['color'], data['alpha'])

if isinstance(coord, coord_flip):
fill_between = ax.fill_betweenx
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotnine/tests/baseline_images/test_geom_boxplot/params.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotnine/tests/baseline_images/test_geom_crossbar/aesthetics.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotnine/tests/baseline_images/test_geom_density/gaussian.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotnine/tests/baseline_images/test_geom_density/triangular.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions plotnine/tests/test_geom_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pandas as pd

from plotnine import ggplot, aes, geom_bar


def test_bar_alpha():
df = pd.DataFrame(
{
"x": ["a%i" % i for i in range(10)],
"y": range(1, 11),
"alpha": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1],
}
)
p = ggplot(df, aes("x")) + geom_bar(
aes(y="y", alpha="alpha"),
stat="identity", color="blue", position="dodge"
)
assert p == "bar_alpha"
2 changes: 1 addition & 1 deletion plotnine/tests/test_geom_boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_aesthetics_coordflip(self):

def test_params():
p = (ggplot(df, aes('x')) +
geom_boxplot(df[:m], aes(y='y'), size=2, notch=True) +
geom_boxplot(df[:m], aes(y='y'), size=2, notch=True, alpha=0.3) +
geom_boxplot(df[m:2*m], aes(y='y'), size=2,
notch=True, notchwidth=0.8) +
# outliers
Expand Down