Python&数学基礎

幾何学模様(直線)


プログラム(Python 3.10.4)


    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    from matplotlib.collections import LineCollection
    import numpy as np
    import pandas as pd


    def geometry_line():
        N = 25
        a = np.linspace(0, 1, N)
        re_a = a[::-1]
        b = np.linspace(0, 1, N)
        re_b = b[::-1]
        p = 0.7
        lines_v = [[(a[i], 0),(a[i], 1)] for i in range(N) if np.random.rand() > p]
        lines_h = [[(0, b[i]),(1, b[i])] for i in range(N) if np.random.rand() > p]
        lines_d1 = [[(a[i], 0),(0, b[i])] for i in range(N) if np.random.rand() > p]
        lines_d2 = [[(1, b[i]),(a[i], 1)] for i in range(N) if np.random.rand() > p]
        lines_d3 = [[(re_a[i], 0),(1, b[i])] for i in range(N) if np.random.rand() > p]
        lines_d4 = [[(0, re_b[i]),(a[i], 1)] for i in range(N) if np.random.rand() > p]
        colors = ["#1f77b4"] * N
        line_collection_v = LineCollection(lines_v, colors=colors, linewidths=1)
        line_collection_h = LineCollection(lines_h, colors=colors, linewidths=1)
        line_collection_d1 = LineCollection(lines_d1, colors=colors, linewidths=1)
        line_collection_d2 = LineCollection(lines_d2, colors=colors, linewidths=1)
        line_collection_d3 = LineCollection(lines_d3, colors=colors, linewidths=1)
        line_collection_d4 = LineCollection(lines_d4, colors=colors, linewidths=1)
        fig_geometry_lines, ax = plt.subplots(figsize=(4,4))
        ax.set_xlim(0, 1)
        ax.set_ylim(0, 1)
        ax.add_collection(line_collection_v)
        ax.add_collection(line_collection_h)
        ax.add_collection(line_collection_d1)
        ax.add_collection(line_collection_d2)
        ax.add_collection(line_collection_d3)
        ax.add_collection(line_collection_d4)
        ax.tick_params(labelbottom=False, labelleft=False, labelright=False, labeltop=False)
        ax.tick_params(bottom=False, left=False, right=False, top=False)
        plt.show()