Created
October 12, 2023 10:41
-
-
Save mjhong0708/5dffdb936ecb82d22a9d9fc86b91096e to your computer and use it in GitHub Desktop.
Draw connected horizontal lines (like free energy diagram)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Author
mjhong0708
commented
Oct 12, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment