Search code examples
matplotlibscatter

Matplotlib add line to connect related scatter points


Is there a built-in way to add a line connecting scatter points with the same y-val?

Currently have this:

x1 = [6, 11, 7, 13, 6, 7.5]
x2 = [np.nan, np.nan, np.nan, np.nan, np.nan, 8.6]
y = [2, 10, 2, 14, 9, 10]
df = pd.DataFrame(data=zip(x1, x2, y), columns=["x1", "x2", "y"])

fig, ax = plt.subplots()
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.scatter(df["x1"], df["y"], c="k")
ax.scatter(df["x2"], df["y"], edgecolor="k", facecolors="none")

enter image description here

Want this:

enter image description here


Solution

  • You can pair the points (x1[i],y[i]) and (x2[i],y[i]) iteratively by using the following code:

    
    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    
    x1 = [6, 11, 7, 13, 6, 7.5]
    x2 = [np.nan, np.nan, np.nan, np.nan, np.nan, 8.6]
    y = [2, 10, 2, 14, 9, 10]
    df = pd.DataFrame(data=zip(x1, x2, y), columns=["x1", "x2", "y"])
    
    fig, ax = plt.subplots()
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.scatter(df["x1"], df["y"], c="k")
    ax.scatter(df["x2"], df["y"], edgecolor="k", facecolors="none")
    for i in range(len(y)):
      plt.plot([x1[i],x2[i]],[y[i],y[i]])
    plt.show()
    
    

    The output of this code gives:

    enter image description here