pythonmatplotlib

Plotting Y data vs X data with alternating colors for each section in Python


I have what might be a simple problem.

I have a dataset that looks like this:

      Exp   Site Hole  Core Type Sect  A/W  ...         Instrument  Instrument group       Text ID   Test No.  Sample comments Test comments Result comments
0     395C  U1563    A     1    H    1  NaN  ...  MS3-V0.0.0.0-0656             WRMSL  SECT11004221  119231751              NaN           NaN             NaN
1     395C  U1563    A     1    H    1  NaN  ...  MS3-V0.0.0.0-0656             WRMSL  SECT11004221  119231751              NaN           NaN             NaN
...
3932  395C  U1563    A    11    H    5  NaN  ...  MS3-V0.0.0.0-0656             WRMSL  SECT11008941  119238791              NaN           NaN             NaN

[3933 rows x 20 columns]

My task is to plot Depth CSF-B (m) vs Magnetic susceptibility (instr. units), but whenever the Sect changes, the line should alternate between black and dark blue.

I tried the following code:

import pandas as pd
import matplotlib.pyplot as plt

td3 = pd.read_csv(
    "C:/Users/data.csv"
)


td3 = td3.dropna(subset=["Depth CSF-B (m)", "Magnetic susceptibility (instr. units)"])
td3 = td3[td3["Sect"] != "cc"]
td3 = td3[td3["Depth CSF-B (m)"] <= 100]


sect_order = td3.groupby("Sect")["Depth CSF-B (m)"].min().sort_values().index


plt.figure(figsize=(8, 5))

colors = ["black", "darkblue"]  

for i, sect in enumerate(sect_order):
    group = td3[td3["Sect"] == sect]

   
    inds = np.argsort(group["Depth CSF-B (m)"].values)

    plt.plot(
        group["Depth CSF-B (m)"].values[inds],
        group["Magnetic susceptibility (instr. units)"].values[inds],
        color=colors[i % 2],
        label=f"Sect {sect}"
    )

plt.xlabel("Depth CSF-B (m)")
plt.ylabel("Magnetic susceptibility (instr. units)")
plt.xlim(0, 100)
plt.title("Magnetic Susceptibility vs Depth (0–100 m)")
plt.legend()
plt.tight_layout()
plt.show()

However, the plot looks messed up because the colors don’t alternate correctly or the lines overlap strangely.

enter image description here

What I want:

How can I correctly plot this in Python using Matplotlib?

For additional questions whenever I just plot the data without the color septeration the data look like this:

enter image description here


Solution

  • Okay with your answers (esspecally @AndrasDeak--СлаваУкраїні) I finally come to this following code:

    what I did: sorting by depth + breaking lines at section boundaries = clean, readable plots (thats new). Without these steps, matplotlib connects points in raw row order, which almost always looks chaotic.

    import pandas as pd
    import matplotlib.pyplot as plt
    
    
    
    td3 = pd.read_csv("data")
    
    td3 = td3.dropna(subset=["Depth CSF-B (m)", "Magnetic susceptibility (instr. units)"])
    td3 = td3[td3["Depth CSF-B (m)"] <= 100]
    td3["Depth CSF-B (m)"] = td3["Depth CSF-B (m)"].astype(float)
    td3["Magnetic susceptibility (instr. units)"] = td3["Magnetic susceptibility (instr. units)"].astype(float)
    td3 = td3.sort_values("Depth CSF-B (m)").reset_index(drop=True)
    
    plt.figure(figsize=(8,5))
    colors = ["black", "darkblue"]
    current_color_idx = 0
    
    x_vals = []
    y_vals = []
    current_sect = td3.iloc[0]["Sect"]
    
    for _, row in td3.iterrows():
        if row["Sect"] != current_sect:
       
            plt.plot(x_vals, y_vals, color=colors[current_color_idx % 2])
            current_color_idx += 1
            x_vals = []
            y_vals = []
            current_sect = row["Sect"]
        
        x_vals.append(row["Depth CSF-B (m)"])
        y_vals.append(row["Magnetic susceptibility (instr. units)"])
    
    plt.plot(x_vals, y_vals, color=colors[current_color_idx % 2])
    
    plt.xlabel("Depth CSF-B (m)")
    plt.ylabel("Magnetic susceptibility (instr. units)")
    plt.xlim(0, 100)
    plt.title("Magnetic Susceptibility vs Depth (0–100 m)")
    plt.tight_layout()
    plt.show()
    

    enter image description here