问题:
我有一个数据集,其中包含x和值y对,lower_limit以及.upper_limity
我想在 plot.ly 散点图中绘制xvs. ,如果≤ ≤ ,则标记为绿色,否则为红色。ylower_limityupper_limit
我知道我可以使用 2 个跟踪,或者color在 DataFrame 中添加一列。但是,我想动态生成这些颜色并只使用一条轨迹。
示例:
考虑这个数据集:
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])
