summaryrefslogtreecommitdiff
path: root/python-graphics/plotter.py
blob: 6ab8275a76f969b3ef27ba0fbcd292272af26e87 (plain)
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
import matplotlib.pyplot as plt
from sympy.plotting import plot
import numpy as np


def plot_preset(data):
    a = data["a"]
    b = data["b"]
    min_x = data["min"]
    max_x = data["max"]

    # 100 linearly spaced numbers
    x = np.linspace(min_x, max_x, 100)

    if data["type"] == "parabola":
        c = data["c"]
        y = a * x**2 + b * x + c
    elif data["type"] == "line":
        y = a * x + b

    # setting the axes at the centre
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.spines['left'].set_position('center')
    ax.spines['bottom'].set_position('zero')
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')

    # plot the function
    plt.plot(x,y, 'r')

    # show the plot
    plt.show()


def plot_points(data):
    points = data["points"]

    x = []
    y = []
    for (xi, yi) in points:
        x.append(xi)
        y.append(yi)

    # setting the axes at the centre
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.spines['left'].set_position('center')
    ax.spines['bottom'].set_position('zero')
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')

    # plot the function
    plt.plot(x,y, data["mode"])

    # show the plot
    plt.show()


def plot_user(data):
    plot(data["expr"])