21.2 Drawing plots on canvas

##
21.2 Drawing plots on canvas

PyChart creates a new canvas object when "`area.T.draw()`" (see Section 6) is called for the first time, and no canvas is yet created at that moment. You can thus use you own canvas by creating a canvas before calling the first `area.T.draw()`, like below:

    can = canvas.init("foo.pdf")
    ...
    ar = area.T(...)
    ar.draw()

You can also achieve the same effect by passing the canvas object to the `area.T.draw()` method explicitly:

    can = canvas.init("foo.pdf")
    ...
    ar = area.T(...)
    ar.draw(can)

You can also pass a file object (or file-like object, such as `StringIO`) to canvas.init. In this case, you need to define the output format via the second argument.

    fd = file("foo.pdf", "w")
    can = canvas.init(fd, "pdf")
    ...
    ar.draw(can)

Naturally, you can write to multiple files by passing multiple `canvas` objects to different `area.T.draw()`. For example, the below example draws the first chart to graph1.pdf and the next chart to graph2.pdf.

../demos/twographs.py

    from pychart import *

    can = canvas.init("graph1.pdf")
    data = chart_data.read_csv("lines.csv")
    ar = area.T(x_range = (0,100), y_range = (0,100),
                x_axis = axis.X(label="X", tic_interval=10),
                y_axis = axis.Y(label="Y", tic_interval=10))
    eb = error_bar.error_bar2(tic_len=5, hline_style=line_style.gray50)
    ar.add_plot(line_plot.T(label="foo", data=data, error_bar=eb, y_error_minus_col=3),
                line_plot.T(label="bar", data=data, ycol=2, error_bar=eb, y_error_minus_col=3))
    ar.draw(can)
    tb = text_box.T(loc=(40, 130), text="This is\nimportant!", line_style=None)
    tb.add_arrow((ar.x_pos(data[6][0]), ar.y_pos(data[6][1])), "cb")
    tb.draw(can)

    can = canvas.init("graph2.pdf")
    ar = area.T(loc=(200, 0), x_range=(0,100), y_range=(0,100),
                x_axis = axis.X(label="X", tic_interval=10),
                y_axis = axis.Y(label="Y", tic_interval=10))
    ar.add_plot(line_plot.T(label="foo", data=data, data_label_format="/8{}%d"),
                line_plot.T(label="bar", data=data, ycol=2))
    ar.draw(can)

    # Note: can.close() is called automatically for every open canvas.