【发布时间】:2019-09-19 10:40:19
【问题描述】:
问题:
我有一个包含x 和y 值对的数据集,以及y 的lower_limit 和upper_limit 值。
我想在 plot.ly 散点图中绘制x 与y,如果lower_limit ≤ y ≤ upper_limit,则标记为绿色,否则为红色强>。
我知道我可以使用 2 个跟踪,或者在 DataFrame 中添加一个 color 列。但是,我想动态生成这些颜色并只使用一条轨迹。
示例:
考虑这个数据集:
x y lower_limit upper_limit
0 1 13 10 15
1 2 13 15 20
2 3 17 15 20
第一个标记 (x=1, y=13) 应该是绿色的,因为 lower_limit ≤ y ≤ upper_limit (10 ≤ 13 ≤ 15),就像第三个一样。
但是第二个应该是红色的,因为y lower_limit.
MWE:
import pandas as pd
import plotly.graph_objs as go
import plotly.plotly as py
import plotly.offline as po
data = [
[1, 13, 10, 15],
[2, 13, 15, 20],
[3, 17, 15, 20]
]
df = pd.DataFrame(
data,
columns=['x', 'y', 'lower_limit', 'upper_limit']
)
trace = go.Scatter(
x=df['x'],
y=df['y'],
mode='markers',
marker=dict(
size=42,
# I want the color to be green if
# lower_limit ≤ y ≤ upper_limit
# else red
color='green',
)
)
po.plot([trace])
【问题讨论】:
标签: python colors plotly plotly-python