Skip to content

Instantly share code, notes, and snippets.

@bbengfort
Created May 29, 2018 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbengfort/3627b14f70492e90572a4f490cc71090 to your computer and use it in GitHub Desktop.
Save bbengfort/3627b14f70492e90572a4f490cc71090 to your computer and use it in GitHub Desktop.
Generates taxicab vs. euclidean distance graphic
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
def draw_distances(path="atap_ch06_euclidean_manhattan_distance.png"):
_, ax = plt.subplots(figsize=(6,6))
points = np.array([
[2, 2], [8, 8]
])
ax.plot([2, 2, 8], [2, 8, 8], c='r', lw=2, label="$D=12$")
ax.plot([2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8], [2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8], c='b', lw=2)
ax.plot(points[:,0], points[:,1], c='g', lw=3, label="$D=\sqrt{72}$")
ax.plot([2, 7, 7, 8, 8], [2, 2, 4, 4, 8], c='m', lw=2)
ax.scatter(points[:,0], points[:,1], s=104, c='k', zorder=7)
majorLocator = MultipleLocator(2)
majorFormatter = FormatStrFormatter('%d')
minorLocator = MultipleLocator(1)
for axis in (ax.xaxis, ax.yaxis):
axis.set_major_locator(majorLocator)
axis.set_major_formatter(majorFormatter)
axis.set_minor_locator(minorLocator)
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.grid(which="major", lw=1)
ax.grid(which="minor", lw=0.5)
ax.set_axisbelow(True)
ax.legend(loc="lower right", frameon=True)
plt.savefig(path)
if __name__ == "__main__":
draw_distances()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment