You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言:基于t值条件为绘图线条设置不同颜色

Solution for Coloring Plot Lines Based on t-values

Got it, let's break this down into actionable solutions based on what you're likely needing. I'll cover two common scenarios: coloring an entire line based on t-value conditions, and coloring segments of the line dynamically based on each point's t-value.

1. Color the Entire Line Based on t-value Conditions

If you want the whole line to be black if any t-value in the column is outside [-2, 2], or gray otherwise (swap any() with all() if you want to check whether all t-values meet the condition):

import matplotlib.pyplot as plt
import pandas as pd

# Assume your first mean column is the 0th index, corresponding t-value column is 1st index
mean_series = df.iloc[:, 0]
t_series = df.iloc[:, 1]

# Determine line color
if ((t_series > 2) | (t_series < -2)).any():
    line_color = "black"
else:
    line_color = "gray"

# Plot the line
plt.plot(mean_series, color=line_color)
plt.xlabel("Index")
plt.ylabel("Mean Value")
plt.title("Mean Line Colored by t-value Range")
plt.show()

2. Color Line Segments Dynamically (Per-Point t-value)

If you want different segments of the line to change color based on the t-value at each position (more granular control):

import matplotlib.pyplot as plt
import pandas as pd

mean_col = df.iloc[:, 0]
t_col = df.iloc[:, 1]

# Create a color column to map each row's t-value to a color
df["line_color"] = ["black" if (t > 2 or t < -2) else "gray" for t in t_col]

# Plot segments by finding color transitions
prev_color = df["line_color"].iloc[0]
start_idx = 0

for idx in range(1, len(df)):
    current_color = df["line_color"].iloc[idx]
    if current_color != prev_color:
        # Draw the segment from start_idx to idx-1
        plt.plot(df.index[start_idx:idx], mean_col[start_idx:idx], color=prev_color)
        start_idx = idx
        prev_color = current_color

# Draw the final segment
plt.plot(df.index[start_idx:], mean_col[start_idx:], color=prev_color)

plt.xlabel("Index")
plt.ylabel("Mean Value")
plt.title("Mean Line with Dynamic Color by t-value")
plt.show()

Bonus: Loop Through All 90 Mean-t Value Pairs

Since you have 90 pairs of columns, you can automate plotting all lines with their respective colors using a loop (assuming mean columns are even-indexed, t-value columns are odd-indexed):

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(12, 8))

# Iterate through each mean-t value pair
for col_idx in range(0, 180, 2):
    mean_series = df.iloc[:, col_idx]
    t_series = df.iloc[:, col_idx + 1]
    
    # Set color based on t-values
    line_color = "black" if ((t_series > 2) | (t_series < -2)).any() else "gray"
    
    # Plot with slight transparency to avoid overlapping clutter
    ax.plot(mean_series, color=line_color, alpha=0.6)

ax.set_xlabel("Index")
ax.set_ylabel("Mean Values")
ax.set_title("90 Mean Lines Colored by Corresponding t-value Ranges")
plt.tight_layout()
plt.show()

内容的提问来源于stack exchange,提问作者Bit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 06:53:17