Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Random graph plotter
import click
import matplotlib.pyplot as plt
import numpy as np
def parse_string_to_int(string):
chars_as_ints = [ord(char) for char in string]
string_as_int = sum(chars_as_ints)
return string_as_int
def create_random_points(string=None):
if string:
int_string = parse_string_to_int(string)
np.random.seed(int_string)
amount = np.random.randint(100, 200)
random_points_x = np.random.normal(0, 1, (amount, 1))
random_points_y = np.random.normal(0, 1, (amount, 1))
random_points = np.concatenate([random_points_x, random_points_y], axis=1)
return random_points
def get_lines(random_points, radius=0.5):
lines = []
for i, point_1 in enumerate(random_points):
for j, point_2 in enumerate(random_points[i:]):
if i == j:
continue
if np.sqrt(sum((point_1 - point_2) ** 2)) < radius:
lines.append(np.array([point_1, point_2]))
return lines
def clean_string(string):
for symbol in [" ", ",", ";", ":", ".", "\n", "\r", "\t"]:
string = string.replace(symbol, "")
return string
def plot_in_axis(ax, string, radius=0.5, size=5):
random_points = create_random_points(string)
lines = get_lines(random_points, radius)
xlims = [np.min(random_points[:, 0]) - radius, np.max(random_points[:, 0]) + radius]
ylims = [np.min(random_points[:, 1]) - radius, np.max(random_points[:, 1]) + radius]
lims = [min([xlims[0], ylims[0]]), max([xlims[1], ylims[1]])]
for line in lines:
ax.plot(line[:, 0], line[:, 1], "-k", alpha=0.4)
ax.scatter(random_points[:, 0], random_points[:, 1], c="k", s=size)
ax.tick_params(
top=False,
bottom=False,
labelbottom=False,
right=False,
left=False,
labelleft=False,
)
ax.set_xlim(*lims)
ax.set_ylim(*lims)
@click.command()
@click.argument("string", default=None, type=str)
@click.option("--radius", default=0.5, type=float)
@click.option("--size", default=5, type=int)
def main(string, radius, size):
_, ax = plt.subplots(1, 1, figsize=(10, 10))
plot_in_axis(ax, string, radius, size)
ax.set_xlabel("\n" + string, fontsize=15)
plt.savefig(f"{clean_string(string)}.jpg", format="jpg", dpi=150)
plt.show()
if __name__ == "__main__":
main() # pylint: disable=no-value-for-parameter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.