Пример #1
0
    def test_lin(self):
        """
        Test a linear function.
        """
        f = figure.Figure(figsize=(50, 15))
        xs = linspace(-1, 1, 50)
        ys = xs
        f.set_x_label("x")
        f.set_y_label("x")
        f.scatter(xs, ys)
        f.show()
        string_expected = \
            "      ᐃ                                           \n" \
            "   1.0├                                   xx      \n" \
            "      │                               xxxx        \n" \
            "   0.6├                           xxxx            \n" \
            "      │                        xxxx               \n" \
            "x  0.2├                    xxxx                   \n" \
            "      │                 xxx                       \n" \
            "  -0.2├             xxxx                          \n" \
            "      │         xxxx                              \n" \
            "  -0.6├      xxxx                                 \n" \
            "      │  xxxx                                     \n" \
            "  -1.0├xx                                         \n" \
            "      └┴────────┴────────┴────────┴────────┴─────ᐅ\n" \
            "       -1.0     -0.5     0.0      0.5      1.0    \n" \
            "                            x                     \n"

        string_tested = f.export_str()
        self.assertEqual(string_expected, string_tested)
Пример #2
0
    def test_cube(self):
        """
        Tests x^3.
        """
        f = figure.Figure(figsize=(30, 10))
        xs = linspace(-1, 1, 200)
        ys = [x * x * x for x in xs]
        f.set_x_label("x")
        f.set_y_label("x*x*x")
        f.scatter(xs, ys)

        string_tested = f.export_str()
        string_expected = \
            "   1.0ᐃ                  x    \n" \
            "x     │                xx     \n" \
            "*     │              xxx      \n" \
            "x  0.0├    xxxxxxxxxxx        \n" \
            "*     │  xxx                  \n" \
            "x     │ xx                    \n" \
            "  -1.0├x                      \n" \
            "      └┴─────┴─────┴─────┴───ᐅ\n" \
            "       -1.0  -0.3  0.3   1.0  \n" \
            "                  x           \n"

        self.assertEqual(string_expected, string_tested)
Пример #3
0
 def test_small(self):
     f = figure.Figure(figsize=(30, 8))
     xs = linspace(-2, 2, 200)
     ys = [x * x for x in xs]
     f.set_x_label("x")
     f.set_y_label("x*x")
     f.scatter(xs, ys)
     f.show()
Пример #4
0
 def test_parabola(self):
     f = figure.Figure(figsize=(60, 15))
     xs = linspace(-2, 2, 200)
     ys = [x * x for x in xs]
     f.set_x_label("x")
     f.set_y_label("x*x")
     f.scatter(xs, ys)
     f.show()
Пример #5
0
 def test_sin(self):
     f = figure.Figure(figsize=(100, 30))
     xs = linspace(-PI, PI, 200)
     ys = [sin(x) for x in xs]
     f.set_x_label("x")
     f.set_y_label("sin(x)")
     f.scatter(xs, ys)
     f.show()
Пример #6
0
 def test_scale_exponent(self):
     f = figure.Figure()
     xs = linspace(-10, 10, 100)
     ys = linspace(-10, 10, 100)
     f.scatter(xs, ys)
     f.set_x_lim()
     f.init_x_scale()
     f.set_x_label("fruit 1")
     f.set_y_label("fruit 2")
     f.set_x_unit("apple")
     f.set_y_unit("banana")
     f.show()
Пример #7
0
    def test_draw_graph_components(self):
        f = figure.Figure(figsize=(50, 15))
        xs = [0, 1, 2, 3, 4, 5]
        ys = [0, 1, 2, 3, 4, 5]
        f.set_x_label("price")
        f.set_y_label("demand")
        f.scatter(xs, ys)

        f.draw_x_axis()
        f.draw_y_axis()
        f.draw_plots()
        f.decorate_axes()
        print(f.draw_canvas())
Пример #8
0
    def test_multi(self):
        """
        Tests putting multiple graphs into one plot.
        """
        f = figure.Figure(figsize=(57, 20))
        xs = linspace(-10, 10, 200)

        ys = [x * x for x in xs]
        f.scatter(xs, ys)

        ys2 = [-x * x for x in xs]
        f.scatter(xs, ys2)

        f.set_x_label("x")
        f.set_y_label("x*x")
        f.set_x_unit("m")
        f.set_y_unit("m^2")
        f.show()
Пример #9
0
    def test_lim_scale(self):
        f = figure.Figure()
        xs = [0, 1, 2, 3, 4]
        ys = [0, 1, 2, 3, 4]
        f.scatter(xs, ys)

        f.set_x_lim()
        self.assertEqual((0, 4.0), f.x_lim)
        f.set_y_lim()
        self.assertEqual((0, 4.0), f.y_lim)

        f.init_x_scale()
        f.init_y_scale()

        print([f.x2bin(x) for x in xs])
        print([f.y2bin(y) for y in ys])

        print(f.get_x_tick_positions())
        print(f.get_x_tick_labels())
Пример #10
0
def plot_file(file: TextIO):
    """
    Plots a file of xy format.
    :param file: file name
    """
    x_data = []
    y_data = []
    for l in file.readlines():
        data = list(map(float, l.split()))
        if len(data) != 2:
            raise ValueError("File must contain only two columns.")

        x_data.append(data[0])
        y_data.append(data[1])

    # create figure
    f = figure.Figure()

    # plot data
    f.scatter(x_data, y_data)
    f.show()
Пример #11
0
 def test_decorate_axes(self):
     f = figure.Figure()
     f.set_x_label("price")
     f.set_y_label("demand")
     print(f.export_str())
Пример #12
0
 def test_width(self):
     f = figure.Figure()
     self.assertEqual(73, f.graph_width)
     self.assertEqual(19, f.graph_height)