Last active
September 4, 2017 13:46
-
-
Save Ogaday/33f1c938eaa65a48e29e5c8f4ffdf197 to your computer and use it in GitHub Desktop.
Plot horizontal and vertical joining lines for coordinates described by arrays `x` & `y`.
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
# coding: utf-8 | |
""" | |
Plot horizontal and vertical joining lines for coordinates described by | |
arrays `x` & `y`. | |
""" | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def lines(x, y, first='x', ax=None, **kwargs): | |
""" | |
Args: | |
`x` & `y`: coordinates suitable for passing to a scatter plot. | |
`first`: `'x'` or `'y'`. Draw lines along this axis first. | |
`ax`: axis on which to plot lines. | |
""" | |
try: | |
assert first in ('x', 'y') | |
except: | |
raise | |
else: | |
if first == 'x': | |
ax.hlines(y[:-1], x[:-1], x[1:], **kwargs) | |
ax.vlines(x[1: ], y[:-1], y[1:], **kwargs) | |
else: | |
ax.hlines(y[1: ], x[:-1], x[1:], **kwargs) | |
ax.vlines(x[:-1], y[:-1], y[1:], **kwargs) | |
return ax | |
if __name__ == "__main__": | |
x = np.random.rand(10).cumsum() | |
y = np.random.rand(10) | |
fig, ax = plt.subplots() | |
ax.scatter(x, y) | |
lines(x, y, first='x', ax=ax, linestyles='dashed', colors='r', alpha=0.5) | |
lines(x, y, first='y', ax=ax, linestyles='dotted', colors='b', alpha=0.5) | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment