plotly 散点图标记的条件格式
Conditional formatting of plotly scatterplot markers
问题:
我有一个包含 x
和 y
值对的数据集,以及 y
.
的 lower_limit
和 upper_limit
值
我想在 plot.ly 散点图中绘制 x
与 y
,并且 如果 lower_limit
≤ [,则将标记涂成绿色=13=] ≤ 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])
我建议创建一个新数组来存储颜色值,请在下方找到使用 np.where
和 np.logical_and
形成条件比较的示例。
import plotly.offline as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot, plot
from plotly import tools
import pandas as pd
import numpy
init_notebook_mode(connected=True)
data = [
[1, 13, 10, 15],
[2, 13, 15, 20],
[3, 17, 15, 20]
]
df = pd.DataFrame(
data,
columns=['x', 'y', 'lower_limit', 'upper_limit']
)
#df['color'] = np.where(np.logical_and(df['lower_limit'] >= df['y'], df['y'] <= df['upper_limit']), 'green', 'red')
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=np.where(np.logical_and(df['lower_limit'] <= df['y'], df['y'] <= df['upper_limit']), 'green', 'red'),
)
)
iplot([trace])
参考文献:
Pandas: np.where with multiple conditions on dataframes
问题:
我有一个包含 x
和 y
值对的数据集,以及 y
.
lower_limit
和 upper_limit
值
我想在 plot.ly 散点图中绘制 x
与 y
,并且 如果 lower_limit
≤ [,则将标记涂成绿色=13=] ≤ 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])
我建议创建一个新数组来存储颜色值,请在下方找到使用 np.where
和 np.logical_and
形成条件比较的示例。
import plotly.offline as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot, plot
from plotly import tools
import pandas as pd
import numpy
init_notebook_mode(connected=True)
data = [
[1, 13, 10, 15],
[2, 13, 15, 20],
[3, 17, 15, 20]
]
df = pd.DataFrame(
data,
columns=['x', 'y', 'lower_limit', 'upper_limit']
)
#df['color'] = np.where(np.logical_and(df['lower_limit'] >= df['y'], df['y'] <= df['upper_limit']), 'green', 'red')
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=np.where(np.logical_and(df['lower_limit'] <= df['y'], df['y'] <= df['upper_limit']), 'green', 'red'),
)
)
iplot([trace])
参考文献:
Pandas: np.where with multiple conditions on dataframes