-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
change line opacity, leave markers opaque #2684
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
Comments
You can set |
I saw that possibility, but it requires to manually set the color of the line as well, rather than letting plotly do the coloring. |
Seems like a reasonable use case... I wouldn't be opposed to adding that.
Have you seen |
awesome.
I had not seen it, but it's exactly what I was looking for. you just made my crooked DIY solution obsolete, thanks! |
I have the same use-case. |
+1 |
+2 |
+3 My use case is the bottom right chart here https://covid-19-raffg.herokuapp.com/ Workaround is this:
|
+4 While experimenting with the histogram and attempting to add lines in overlay bar mode from the seaborn mpg sample dataset, i.e.: mpg = sns.load_dataset('mpg')
fig = px.histogram(mpg, x='mpg', color='origin', barmode='overlay', opacity=0.33, marginal='box')
fig.update_traces(marker_line_width=2) The default isn't great as the opacity applies to the default of black lines, meanwhile, notice that the default behaviour for the marginal box plot does exactly what most users would aim at - same color lines as the filled object. So my view is that it's quite inconsistent from a theme perspective to not be able to match this with the lines for the bar plot. I attempted a similar workaround to @raffg , but seems that with the histogram, the alpha channel set for line color is ignored and overridden by the opacity value regardless. def col_hex_to_to_rgba(rgb_hex: str, alpha: float=1.0) -> tuple[int, int, int, float]:
return tuple([int(rgb_hex.lstrip('#')[i:i+2], 16) for i in (0, 2, 4)] + [alpha])
fig = px.histogram(mpg, x='mpg', color='origin', barmode='overlay', opacity=0.33, marginal='box')
for trace in fig.data:
# Copy marker colour in hex string format (without alpha channel) and convert it to and RGBA string (adding the alpha channel)
trace.marker.line.color = f'rgba{col_hex_to_to_rgba(rgb_hex=trace.marker.color, alpha=1.0)}'
trace.marker.line.width = 2
fig Disappointingly the lines only become visible when the colors/edges overlap. This also suggests that the graphic renders the lines outwards in way that overlaps, which is not ideal. For a histogram plot, line thickness rendered inwards would be better I think, such that the color blending from neighboring bar lines don't affect each other. Also, checked that data of the figure traces, and indeed, alpha for the lines is being ignored. It was set correctly, e.g. content of Histogram({
'alignmentgroup': 'True',
'bingroup': 'x',
'hovertemplate': 'origin=usa<br>mpg=%{x}<br>count=%{y}<extra></extra>',
'legendgroup': 'usa',
'marker': {'color': '#636efa',
'line': {'color': 'rgba(99, 110, 250, 1.0)', 'width': 2},
'opacity': 0.33,
'pattern': {'shape': ''}},
'name': 'usa',
'offsetgroup': 'usa',
'orientation': 'v',
'showlegend': True,
'x': array([18., 15., 18., ..., 32., 28., 31.]),
'xaxis': 'x',
'yaxis': 'y'
}) |
I've found this seems to be an issue for scatter-plots as well, and the above mentioned work-around using the alpha channel with RGBA does not work. opacity at the parent marker level seems to override any alpha value set for the marker line color. E.g. when trying to create a bubble chart with transparent fill and solid lines: import plotly.express as px
df_gap_2007 = px.data.gapminder().query('year == 2007')
fig = px.scatter(df_gap_2007, y='lifeExp', x='gdpPercap',
size='pop', size_max=50,
opacity=0.25, color='continent',
hover_name='country', hover_data=['lifeExp', 'gdpPercap', 'pop']
)
for trace in fig.data:
trace.marker.line.color = 'rgba(0, 0, 0, 1.0)'
trace.marker.line.width = 2
fig.show() Resulting plot does not have solid black lines as would be expected with Again, the marker part within
|
Hi - this issue has been sitting for a while, so as part of our effort to tidy up our public repositories I'm going to close it. If it's still a concern, we'd be grateful if you could open a new issue (with a short reproducible example if appropriate) so that we can add it to our stack. Cheers - @gvwilson |
Is it possible to add an opacity to the line-attribute?change the line opacity but not the marker opacity?
I found that I can set the opacity of the entire line including markers (
opacity = .5
) and the one of the marker (e.g.marker={"opacity":1}
). However, it is seemingly impossible to reduce the opacity of just the line.As shown in this example:
See result here:

My problem is the following: My data points are important, the lines are just visual aid. I want to make the lines .5-opaque but have the markers fully opaque.
However, when I set
opacity=.5, marker={'opacity':1}
the opacity of the marker is also reduced. (I believe that the marker-opacity is defined in the range[0, line-opacity]
.PS: If there is an existing solution/workaround for this issue, it might be interesting to (also) answer on my post on Stackoverflow
The text was updated successfully, but these errors were encountered: