Skip to content

Instantly share code, notes, and snippets.

@vincentchu
Created June 14, 2013 04:12

Revisions

  1. vincentchu created this gist Jun 14, 2013.
    42 changes: 42 additions & 0 deletions matplotlib.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@

    rcOpts = {'figure.figsize': (6.0,4.0),
    # play nicely with white background in the Qt and notebook frontend
    'figure.facecolor': 'white',
    'figure.edgecolor': 'white',
    # 12pt labels get cutoff on 6x4 logplots, so use 10pt.
    'font.size': 10,
    # 72 dpi matches SVG/qtconsole
    # this only affects PNG export, as SVG has no dpi setting
    'savefig.dpi': 72,
    # 10pt still needs a little more room on the xlabel:
    'figure.subplot.bottom' : .125
    }

    from io import BytesIO
    import base64
    import matplotlib
    # matplotlib.interactive(True)
    matplotlib.interactive(False)

    from matplotlib import pyplot
    pyplot.rcParams.update(rcOpts)

    from pylab import *
    from matplotlib._pylab_helpers import Gcf

    plot([1,2,3])

    fig = Gcf.get_all_fig_managers()[0].canvas.figure

    fc = fig.get_facecolor()
    ec = fig.get_edgecolor()

    bytes_io = BytesIO()
    fig.canvas.print_figure(bytes_io, format='png', bbox_inches='tight', facecolor=fc, edgecolor=ec)
    data = bytes_io.getvalue()

    print("DATA = %s" %(base64.b64encode(data)))