Skip to content

Instantly share code, notes, and snippets.

@mjhong0708
Created October 12, 2023 10:41
Show Gist options
  • Save mjhong0708/5dffdb936ecb82d22a9d9fc86b91096e to your computer and use it in GitHub Desktop.
Save mjhong0708/5dffdb936ecb82d22a9d9fc86b91096e to your computer and use it in GitHub Desktop.
Draw connected horizontal lines (like free energy diagram)
def lineplot(x, y, length=None, width=1, connection_style="--", ax=None, **kwargs):
if ax is None:
ax = plt.gca()
if length is None:
length = length or (max(x) - min(x)) / len(x) * 0.8
x, y = np.array(x), np.array(y)
xmin, xmax = x - length / 2, x + length / 2
lines = ax.hlines(y=y, xmin=xmin, xmax=xmax, **kwargs)
for i in range(len(x) - 1):
xs, ys = [xmax[i], xmin[i + 1]], [y[i], y[i + 1]]
ax.plot(xs, ys, connection_style, color=lines.get_color()[0], linewidth=width)
return ax
@mjhong0708
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment