Skip to content

Instantly share code, notes, and snippets.

@barrysmyth
Last active November 21, 2023 16:33
Show Gist options
  • Save barrysmyth/473d60bb8bd533e9ab5dc1d687925b62 to your computer and use it in GitHub Desktop.
Save barrysmyth/473d60bb8bd533e9ab5dc1d687925b62 to your computer and use it in GitHub Desktop.
plot_heat_line_with_border.py
def plot_heatline_with_border(ax, x, y, c, w, bw=4, bc='midnightblue'):
"""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.
bw - the border thisckness.
bc - the border colour.
"""
# 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 border line, which is thicker than the main heatline.
border_line = LineCollection(segments, colors=bc, linewidths=w+bw, capstyle='round')
ax.add_collection(border_line)
# Build the heat line; each segment with teh segment colors and widths.
heat_line = LineCollection(segments, colors=c, linewidths=w, capstyle='round')
ax.add_collection(heat_line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment