Saturday 31 August 2013

How To Plot Data From C Using Matplotlib

Following on from my earlier blog entitled "How To Pass A C Array To Python Solution" (http://realgonegeek.blogspot.co.uk/2013/08/how-to-pass-c-array-to-python-solution.html) here is some simple code to plot the same data using Matplotlib.

Note : While this solution worked, I found that it was very slow because of the time required to load Python so I ended up reverting back to GNUPlot, see here for further details : http://realgonegeek.blogspot.co.uk/2014/01/interfacing-cc-to-gnuplot.html.

All you need to do is take the earlier C example, change the Python method from PrintArray to PlotArray and recompile.

def PlotArray (a):
    import matplotlib.pyplot as pp
    from matplotlib.font_manager import FontProperties
    import numpy as np
    
    p1, = pp.plot (a, label = "line 1", color='red')

    ax = pp.subplot (111)
    box = ax.get_position ()
    ax.set_position ([box.x0, box.y0, box.width * 0.85, box.height]) # Shink x axis to fit legend
    fontP = FontProperties ()
    fontP.set_size ('small')

    pp.legend (loc=2, prop = fontP, fancybox = True, bbox_to_anchor=(1, 1.0))
    pp.title ("Title")
    pp.xlabel ("X-Axis")
    pp.ylabel ("Y-Axis")
    pp.ylim (ymin = 0, ymax = 10)
    pp.grid (True)

    pp.show (block=True)

    c = 0
    return c

If you have found this solution useful then please do hit the Google (+1) button so that others may be able to find it as well.
Numerix-DSP Libraries : http://www.numerix-dsp.com/eval/

No comments:

Post a Comment