Plotting magnetic susceptibility vs depth with alternating colors for each section in Python
04:20 15 Dec 2025

I have what might be a simple problem, but I’ve been stuck on it for several days.

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")

# Clean data
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]
td3 = td3.sort_values("Depth CSF-B (m)")

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

colors = ["black", "darkblue"]  
Sect_values = td3["Sect"].unique()

for i, sect in enumerate(Sect_values):
    group = td3[td3["Sect"] == sect]
    plt.plot(
        group["Depth CSF-B (m)"],
        group["Magnetic susceptibility (instr. units)"],
        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.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:

  • A continuous plot for the first 100 m of depth.

  • Each Sect is plotted in alternating colors (black / dark blue).

  • No breaks in the lines within a Sect.

How can I correctly plot this in Python using Matplotlib?

python matplotlib