plotly.py:更改线条不透明度,使标记不透明 [英] plotly.py: change line opacity, leave markers opaque
问题描述
是否可以更改线条不透明度而不是标记不透明度?我发现我可以设置整行的不透明度,包括标记(opacity = .5
)和标记之一(例如 marker={"opacity":1}
).
如本例所示:
plotly 导入导入 plotly.graph_objs as goplotly.offline.init_notebook_mode(connected=True) # 我在 jupyter notebook 中运行x = np.arange(0,10)ys = [np.random.rand(10) for _ in range(3)]行 = []对于 y 中的 y:line = go.Scatter(x=x, y=y, mode="markers+lines", opacity=.5, marker={'symbol': 'x', 'size': "15", "opacity":1})行.追加(行)fig = go.Figure(数据=行,布局=go.Layout(showlegend=True))plotly.offline.iplot(fig)
在此处查看结果:
我的问题如下:我的数据点很重要,线条只是视觉辅助.我想让线条 .5 不透明,但让标记完全不透明.
但是,当我设置 opacity=.5, marker={'opacity':1}
时,标记的不透明度也会降低.(我相信标记不透明度是在 [0, line-opacity]
范围内定义的.
有什么方法可以获得线条的颜色并调整其不透明度(甚至在创建线条之后,但在绘制之前).我知道我可以创建两条轨迹,一条是点,一条是线.但是,我希望它们具有相同的颜色,而无需手动指定颜色.(轨迹的数量是不同的,所以我更喜欢坚持分配不同颜色的标准机制)
编辑:我目前的解决方案是将线宽设置为 0.5,这样看起来更好,但显然这个解决方案对我有用,可能对人们没有用谁想要粗体和不那么不透明的线条.
EDIT:关于这个问题/功能请求/行为的 Github 问题:
Is it possible to 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}
).
As shown in this example:
import plotly
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=True) # I'm running in a jupyter notebook
x = np.arange(0,10)
ys = [np.random.rand(10) for _ in range(3)]
lines = []
for y in ys:
line = go.Scatter(x=x, y=y, mode="markers+lines", opacity=.5, marker={'symbol': 'x', 'size': "15", "opacity":1})
lines.append(line)
fig = go.Figure(
data=lines,
layout=go.Layout(showlegend=True)
)
plotly.offline.iplot(fig)
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]
.
Is there any way I can get the colour of the line and adjust its opacity (perhaps even after creating the line, but before plotting it). I know that I could create two traces, one with the points and one with the lines. However, I would like them to be the same colour without having to manually specify the colours. (The number of traces is varying, so I prefer sticking to the standard mechanism that assigns the different colours)
EDIT: My current solution is to set the line-width to 0.5 so it looks better, but obviously this solution works for me and might not be useful to people who want bold and less opaque lines.
EDIT: Github issue concerning this problem/feature request/behaviour: https://github.com/plotly/plotly.js/issues/2684
I create three random lines to but simplify the answer down, one could use:
line=dict(color='rgba(255, 0, 0, 0.5)')
- example from @david Parks
Line objects don't have opacity attributes but you can assign colors using RGBA and set the alpha. You have to assign a color for the markers using RGBA or it will inherit from the line.
See the following for an example:
import plotly
import numpy as np
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=True)
x = np.arange(0,10)
ys = [np.random.rand(10) for _ in range(3)]
lines = []
dots = []
for y in ys:
my_color = ('rgba('+str(np.random.randint(0, high = 256))+','+
str(np.random.randint(0, high = 256))+','+
str(np.random.randint(0, high = 256)))
line = go.Scatter(
x=x,
y=y,
mode="lines+markers",
marker={
'symbol': 'x',
'size': "15",
'color':my_color+',1)'
},
line={
"color":my_color+',0.2)'
}
)
lines.append(line)
fig = go.Figure(
data=lines,
layout=go.Layout(showlegend=True)
)
plotly.offline.iplot(fig)
这篇关于plotly.py:更改线条不透明度,使标记不透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!