情节:如何在指定条件的线图上显示不同的颜色/线段?

发布于 2025-02-07 03:18:07 字数 1451 浏览 1 评论 0原文

我正在尝试绘制基于特定条件的线颜色和线本身(即使用仪表盘或虚线)区分线颜色的线路图。 该图没有显示线条上的线/变化,但是当我徘徊在图表上以及传奇中时,我可以看到图颜色的颜色变化。

fig = px.line(df2, x=df2['Time_Stamp'], y=df2['Blob_1_Prob_48H'])
count = 0
for j, y in enumerate(df2.Blob_1_Prob_48H):
    if count==0:
        count +=1
        fig.add_traces(go.Scatter(
        x = [df2['Time_Stamp'][j]],
        y = [df2['Blob_1_Prob_48H'][j]],
        mode = "lines+markers",
        name = "48H",                    
        text= df2.Name,
        hovertemplate="Prediction Percentage: %{y}<br>Date &Time: %{x}<br>Name: %{text}<extra></extra>"))
    else:
        if df2['Blob_1_Prob_48H'][j] < df2['Blob_1_Prob_48H'][j-1]:
            fig.add_traces(go.Scatter(
                x = [df2['Time_Stamp'][j]],
                y = [df2['Blob_1_Prob_48H'][j]],
                mode = 'lines',
                name = "48H",    
                line = dict(color='red',width=5, dash='dot'),                
                text= df2.Name,
                hovertemplate="Prediction Percentage: %{y}<br>Date &Time: %{x}<br>Name: %{text}<extra></extra>"))
        
fig.update_layout(title="ALEX(S)",xaxis_title='Date & time',yaxis_title='Blob percentage',yaxis_range=[0,105],showlegend=True,width=1200,
                  height=600)
fig.show()

image

I'm trying to plot a line chart that differentiates the line color and the line itself(i.e. using a dash or dotted lines) based on a specific condition.
The figure isn't showing the lines/change in line color but I could see the change in the color of data points on the plot when I hovered over the chart and also in the legend.

fig = px.line(df2, x=df2['Time_Stamp'], y=df2['Blob_1_Prob_48H'])
count = 0
for j, y in enumerate(df2.Blob_1_Prob_48H):
    if count==0:
        count +=1
        fig.add_traces(go.Scatter(
        x = [df2['Time_Stamp'][j]],
        y = [df2['Blob_1_Prob_48H'][j]],
        mode = "lines+markers",
        name = "48H",                    
        text= df2.Name,
        hovertemplate="Prediction Percentage: %{y}<br>Date &Time: %{x}<br>Name: %{text}<extra></extra>"))
    else:
        if df2['Blob_1_Prob_48H'][j] < df2['Blob_1_Prob_48H'][j-1]:
            fig.add_traces(go.Scatter(
                x = [df2['Time_Stamp'][j]],
                y = [df2['Blob_1_Prob_48H'][j]],
                mode = 'lines',
                name = "48H",    
                line = dict(color='red',width=5, dash='dot'),                
                text= df2.Name,
                hovertemplate="Prediction Percentage: %{y}<br>Date &Time: %{x}<br>Name: %{text}<extra></extra>"))
        
fig.update_layout(title="ALEX(S)",xaxis_title='Date & time',yaxis_title='Blob percentage',yaxis_range=[0,105],showlegend=True,width=1200,
                  height=600)
fig.show()

Image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左岸枫 2025-02-14 03:18:07

当然有更好的方法可以做到这一点,但是这是我将列表分为许多增加和减少曲线的代码,然后用不同的样式绘制它们:

import plotly.graph_objects as go
import random

xs = [i for i in range(100)]
ys = [random.randint(1,30) for i in range(100)]

# Temporary lists to store each consecutive increasing or decreasing values
x_inc = []
y_inc = []
x_dec = []
y_dec = []
# List to gather all increasing and decreasing curves
xs_inc = []
ys_inc = []
xs_dec = []
ys_dec = []
# Indicate if it is the first point of new list
first_inc = True
first_dec = True
# Looping over original values
for i in range(len(ys)-1):
    if ys[i+1] < ys[i]:
        if first_inc: # If it is the first, add initial point...
            x_dec.append(xs[i])
            y_dec.append(ys[i])
            if y_inc: # ... and add other to list, if not empty
                xs_inc.append(x_inc)
                ys_inc.append(y_inc)
        x_dec.append(xs[i+1])
        y_dec.append(ys[i+1])
        first_inc = False

        # Restarting all
        x_inc = []
        y_inc = []
        first_dec = True
    else:
        if first_dec: # If it is the first, add initial point...
            x_inc.append(xs[i])
            y_inc.append(ys[i])
            if y_dec: # ... and add other to list, if not empty
                xs_dec.append(x_dec)
                ys_dec.append(y_dec)
        x_inc.append(xs[i+1])
        y_inc.append(ys[i+1])
        first_dec = False

        # Restarting all
        x_dec = []
        y_dec = []
        first_inc = True

fig = go.Figure()

# Plotting all increasing curves
for i, (x, y) in enumerate(zip(xs_inc,ys_inc)):
    fig.add_trace(go.Scatter(x=x,
                            y=y, 
                            name = 'Increase',
                            legendgroup = 'increase', # Group them together
                            showlegend=(True if i == 0 else False), # Only add legend to the first
                            line = dict(color='blue',width=3, dash='dash'),
                            mode="lines",
                            ))
# Plotting all decreasing curves
for i, (x, y) in enumerate(zip(xs_dec,ys_dec)):
    fig.add_trace(go.Scatter(x=x,
                            y=y, 
                            name = 'Decrease',
                            legendgroup = 'decrease', # Group them together
                            showlegend=(True if i == 0 else False), # Only add legend to the first
                            line = dict(color='red',width=3, dash='dot'),
                            mode="lines",
                            ))

fig.show()

输出:

如果您有大量数字,则应适应Numpy或Pandas。

There are certainly better ways to do this, but here is one code where I separate a list into many increasing and decreasing curves and then plot them with different styles:

import plotly.graph_objects as go
import random

xs = [i for i in range(100)]
ys = [random.randint(1,30) for i in range(100)]

# Temporary lists to store each consecutive increasing or decreasing values
x_inc = []
y_inc = []
x_dec = []
y_dec = []
# List to gather all increasing and decreasing curves
xs_inc = []
ys_inc = []
xs_dec = []
ys_dec = []
# Indicate if it is the first point of new list
first_inc = True
first_dec = True
# Looping over original values
for i in range(len(ys)-1):
    if ys[i+1] < ys[i]:
        if first_inc: # If it is the first, add initial point...
            x_dec.append(xs[i])
            y_dec.append(ys[i])
            if y_inc: # ... and add other to list, if not empty
                xs_inc.append(x_inc)
                ys_inc.append(y_inc)
        x_dec.append(xs[i+1])
        y_dec.append(ys[i+1])
        first_inc = False

        # Restarting all
        x_inc = []
        y_inc = []
        first_dec = True
    else:
        if first_dec: # If it is the first, add initial point...
            x_inc.append(xs[i])
            y_inc.append(ys[i])
            if y_dec: # ... and add other to list, if not empty
                xs_dec.append(x_dec)
                ys_dec.append(y_dec)
        x_inc.append(xs[i+1])
        y_inc.append(ys[i+1])
        first_dec = False

        # Restarting all
        x_dec = []
        y_dec = []
        first_inc = True

fig = go.Figure()

# Plotting all increasing curves
for i, (x, y) in enumerate(zip(xs_inc,ys_inc)):
    fig.add_trace(go.Scatter(x=x,
                            y=y, 
                            name = 'Increase',
                            legendgroup = 'increase', # Group them together
                            showlegend=(True if i == 0 else False), # Only add legend to the first
                            line = dict(color='blue',width=3, dash='dash'),
                            mode="lines",
                            ))
# Plotting all decreasing curves
for i, (x, y) in enumerate(zip(xs_dec,ys_dec)):
    fig.add_trace(go.Scatter(x=x,
                            y=y, 
                            name = 'Decrease',
                            legendgroup = 'decrease', # Group them together
                            showlegend=(True if i == 0 else False), # Only add legend to the first
                            line = dict(color='red',width=3, dash='dot'),
                            mode="lines",
                            ))

fig.show()

Output:
decreasing and increasing curves

If you have large amount of numbers, you should adapt to numpy or pandas, for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文