Plotly:分散条件颜色格式问题
Plotly: scatter conditional color formatting issues
知道为什么在模式 = 'lines' 激活时条件颜色格式在这里不起作用吗?
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, 0.5, True],
[2, 0.7, True],
[3, -0.1, False],
[4, -0.3, False],
[5, -0.5, False],
]
df = pd.DataFrame(
data,
columns=['x', 'y', 'Above 0']
)
trace = go.Scatter(
x=df['x'],
y=df['y'],
#mode='markers',
mode='lines', #<--- ISSUE HERE. #########################################
marker=dict(
# I want the color to be green if
# lower_limit ≤ y ≤ upper_limit
# else red
color=np.where(df['Above 0'], 'green', 'red'),
)
)
iplot([trace])
最终的想法是在 0 附近绘制一个振荡器,在 0 上方向前填充绿色,在 0 下方填充红色。
像这样:
据我所知,plotly 不支持一条单线轨迹使用不同的颜色或颜色渐变。如果您使用:
mode='markers+lines'
那么您将获得:
如您所见,不同的颜色 应用于不同的标记。那是因为您的代码段:
marker=dict(
# I want the color to be green if
# lower_limit ≤ y ≤ upper_limit
# else red
color=np.where(df['Above 0'], 'green', 'red'),
)
... 仅适用于标记,对任何行都没有影响。我认为这是有道理的,因为 color=np.where(df['Above 0'], 'green', 'red')
中的颜色毕竟是 markers
的属性,而不是 line
.
的属性
- 鉴于您希望实现的目标。零以上绿色填充,零以下红色填充,两条迹线
- 已使用 plotly express 而不是 graph objects 来简化代码
- 已使用https://numpy.org/doc/stable/reference/generated/numpy.interp.html展开点,以便y==0切换在数据集内
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
data = [
[1, 0.5, True],
[2, 0.7, True],
[3, -0.1, False],
[4, -0.3, False],
[5, -0.5, False],
]
df = pd.DataFrame(data, columns=["x", "y", "Above 0"])
# interpolate out line so that when it goes across y=0 gap is minimised
xn = np.linspace(df["x"].min(), df["x"].max(), len(df) * 25)
df2 = pd.DataFrame({"x": xn, "y": np.interp(xn, df["x"], df["y"])})
px.line(
df2,
x="x",
y="y",
color=df2["y"] > 0,
color_discrete_map={True: "green", False: "red"},
).update_traces(fill="tozeroy", showlegend=False)
知道为什么在模式 = 'lines' 激活时条件颜色格式在这里不起作用吗?
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, 0.5, True],
[2, 0.7, True],
[3, -0.1, False],
[4, -0.3, False],
[5, -0.5, False],
]
df = pd.DataFrame(
data,
columns=['x', 'y', 'Above 0']
)
trace = go.Scatter(
x=df['x'],
y=df['y'],
#mode='markers',
mode='lines', #<--- ISSUE HERE. #########################################
marker=dict(
# I want the color to be green if
# lower_limit ≤ y ≤ upper_limit
# else red
color=np.where(df['Above 0'], 'green', 'red'),
)
)
iplot([trace])
最终的想法是在 0 附近绘制一个振荡器,在 0 上方向前填充绿色,在 0 下方填充红色。
像这样:
据我所知,plotly 不支持一条单线轨迹使用不同的颜色或颜色渐变。如果您使用:
mode='markers+lines'
那么您将获得:
如您所见,不同的颜色 应用于不同的标记。那是因为您的代码段:
marker=dict(
# I want the color to be green if
# lower_limit ≤ y ≤ upper_limit
# else red
color=np.where(df['Above 0'], 'green', 'red'),
)
... 仅适用于标记,对任何行都没有影响。我认为这是有道理的,因为 color=np.where(df['Above 0'], 'green', 'red')
中的颜色毕竟是 markers
的属性,而不是 line
.
- 鉴于您希望实现的目标。零以上绿色填充,零以下红色填充,两条迹线
- 已使用 plotly express 而不是 graph objects 来简化代码
- 已使用https://numpy.org/doc/stable/reference/generated/numpy.interp.html展开点,以便y==0切换在数据集内
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
data = [
[1, 0.5, True],
[2, 0.7, True],
[3, -0.1, False],
[4, -0.3, False],
[5, -0.5, False],
]
df = pd.DataFrame(data, columns=["x", "y", "Above 0"])
# interpolate out line so that when it goes across y=0 gap is minimised
xn = np.linspace(df["x"].min(), df["x"].max(), len(df) * 25)
df2 = pd.DataFrame({"x": xn, "y": np.interp(xn, df["x"], df["y"])})
px.line(
df2,
x="x",
y="y",
color=df2["y"] > 0,
color_discrete_map={True: "green", False: "red"},
).update_traces(fill="tozeroy", showlegend=False)