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" ( 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 :

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) (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 :

1 comment:

  1. Very Impressive Python tutorial. The content seems to be pretty exhaustive and excellent and will definitely help in learning Python. I'm also a learner taken up Python training and I think your content has cleared some concepts of mine. While browsing for Python tutorials on YouTube i found this fantastic video on Python. Do check it out if you are interested to know more.:-