File size: 3,258 Bytes
84d8880
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import plotly.graph_objs as go
from plotly.subplots import make_subplots

from ._scribble import series


# Create the figure with subplots: 2 rows, 1 column
def __create_fig():
    fig = make_subplots(
        rows=2,
        cols=1,
        row_heights=[0.5, 0.5],  # Top half and bottom half should be equal
        vertical_spacing=0.1,  # Some spacing between the top and bottom sections
        subplot_titles=["", ""],  # No title for the top subplot
    )
    return fig


def __create_annotation(fig, word):
    # Add the upside-down word as an annotation in the top subplot
    fig.add_annotation(
        x=0.5,  # X position (center)
        y=1,  # Y position at the top of the subplot
        text=word,  # The word to display
        showarrow=False,  # No arrow needed
        font=dict(size=20, color="blue"),  # Text styling: large, blue font
        textangle=180,  # Rotate text by 180 degrees (upside down)
        align="center",  # Center alignment
        valign="middle",  # Vertical alignment (centered)
        row=1,
        col=1,  # Place in the top subplot
    )


def __remove_axis(fig):
    # Update layout settings
    fig.update_layout(
        plot_bgcolor="white",  # White background for clean look
        paper_bgcolor="white",  # White paper background
        showlegend=False,  # No legend needed
        margin=dict(l=30, r=30, t=30, b=30),  # Margins around the plot
    )

    # Update axes settings for the top subplot (hide grid, ticks, and lines)
    fig.update_xaxes(
        row=1,
        col=1,
        showgrid=False,
        zeroline=False,
        showticklabels=False,
        showline=False,
    )
    fig.update_yaxes(
        row=1,
        col=1,
        showgrid=False,
        zeroline=False,
        showticklabels=False,
        showline=False,
    )

    # Update axes settings for the bottom subplot (show gridlines and labels)
    fig.update_xaxes(
        row=2,
        col=1,
        showgrid=True,
        zeroline=False,
        showticklabels=False,
        showline=False,
    )
    fig.update_yaxes(
        row=2,
        col=1,
        showgrid=True,
        zeroline=False,
        showticklabels=False,
        showline=False,
    )
    return fig


def __plot_letters(fig, d):
    # Create traces for each segment
    traces = []

    for segment in d:
        trace = go.Scatter(
            x=segment.real,  # Real part of the complex number
            y=segment.imag,  # Imaginary part of the complex number
            mode="markers+lines",  # Display both markers and lines
            showlegend=False,
            line={"width": 3, "color": "blue"},  # Line style
            marker={"size": 3, "color": "blue"},  # Marker style
        )
        traces.append(trace)

    # Add all traces to the bottom subplot
    for trace in traces:
        fig.add_trace(trace, row=2, col=1)


def create(name, fct, event, n=100):
    # Create the figure with subplots: 2 rows, 1 column
    fig = __create_fig()
    __create_annotation(fig, word=f"{name}<br>{fct}<br>{event}")

    segments = list(series(name, n=n, str=fct))

    # d is now a list of list. Flatten it
    # d = list(chain.from_iterable(d))

    __plot_letters(fig, segments)
    __remove_axis(fig)
    return fig