Skip to content

Instantly share code, notes, and snippets.

@barrysmyth
Last active November 21, 2023 15:33
Show Gist options
  • Save barrysmyth/c322631815a5206f69d7237b4a410927 to your computer and use it in GitHub Desktop.
Save barrysmyth/c322631815a5206f69d7237b4a410927 to your computer and use it in GitHub Desktop.
def plot_heatline(ax, x, y, c, w):
"""Plot a colour-coded segmented line graph with a border.
Args:
ax - the current axis.
x - x-axis values.
y - y-axis values.
c - the colours for the segments in the line graph defined by x, y.
w - the line widths for the segments in the line graph defined by x, y.
"""
# Reformat the x, y data to define the segments needed by LineCollection.
points = np.array([x, y]).T.reshape(-1, 1, 2) # Create Nx1x2 array
segments = np.concatenate(
[points[:-1], points[1:]]
, axis=1
) # Pairs of (x,y) points for each segment.
# Build the heat line; each segment with teh segment colors and widths.
heat_line = LineCollection(segments, colors=c, linewidths=w)
ax.add_collection(heat_line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment