python - plotly area chart, how can I set fill opacity?
问题描述
The code below is copied from plotly tutorial https://plot.ly/python/filled-area-plots/, except the line with opacity
setting.
However, this doesn't work. How can I set the opacity of the filled area?
import plotly.plotly as py
import plotly.graph_objs as go
# Add original data
x = ['Winter', 'Spring', 'Summer', 'Fall']
trace0 = dict(
x=x,
y=[40, 60, 40, 10],
hoverinfo='x+y',
mode='lines',
##########
opacity = 0.5,
##########
line=dict(width=0.5,
color='rgb(131, 90, 241)'),
stackgroup='one'
)
trace1 = dict(
x=x,
y=[20, 10, 10, 60],
hoverinfo='x+y',
mode='lines',
##########
opacity = 0.5,
##########
line=dict(width=0.5,
color='rgb(111, 231, 219)'),
stackgroup='one'
)
trace2 = dict(
x=x,
y=[40, 30, 50, 30],
hoverinfo='x+y',
mode='lines',
##########
opacity = 0.5,
##########
line=dict(width=0.5,
color='rgb(184, 247, 212)'),
stackgroup='one'
)
data = [trace0, trace1, trace2]
fig = dict(data=data)
py.iplot(fig, filename='stacked-area-plot-hover', validate=False)
解决方案
You can set the opacity in a filled area plot by providing a RGBA color in fillcolor
, e.g.
import plotly.plotly as py
x = ['Winter', 'Spring', 'Summer', 'Fall']
y_values = [[40, 60, 40, 10],
[20, 10, 10, 60],
[40, 30, 50, 30]]
colors = ['rgba(131, 90, 241, 0.25)',
'rgba(111, 231, 219, 0.5)',
'rgba(184, 247, 212, 1)']
data = []
for i, y in enumerate(y_values):
data.append(dict(x=x,
y=y,
hoverinfo='x+y',
mode='lines',
line=dict(width=0.5,
color=colors[i]),
fillcolor=colors[i],
stackgroup='one'))
fig = dict(data=data)
py.iplot(fig, filename='stacked-area-plot-hover', validate=False)
gives you
推荐阅读
- angular - 在Angular中的另一个页面中锚定滚动
- python - 在 x0 中计算的同一函数的多个导数
- php - 使用阿拉伯语导入 Calibri 字体不起作用
- solidity - 如何在 Solidity 中编写国际象棋游戏?
- python - 如何阻止 Fiddler 访问我的应用程序 API 或在 Python 的后台进程中运行
- java - 如何为每个附加标签运行一次 TestNG 场景?
- java - 如何在 Hikari CP DEBUG 日志中隐藏连接密码
- ruby - 运行“bundle exec rspec”时出现问题
- json - 获取 API Django
- frontend - 仅用于前端的 CMS