Plotting Graphs using Gnuplot

Discussion in 'Python' started by arslanburney@gmail.com, Jun 12, 2008.

  1. Guest

    Hello. Was trying to create a simple plotting function. Wasnt working
    however. If i write the same code without putting it inside a function
    it works. :S. Could some1 tell me the problem? Heres the code:


    # File name Plotting2

    import Gnuplot

    def plot(original, expected, actual):


    if type (original) != type([]):
    return False

    else:

    gp = Gnuplot.Gnuplot()
    gp('set data style lines')


    # Make the plot items
    plot1 = Gnuplot.PlotItems.Data(original, title="Original")
    plot2 = Gnuplot.PlotItems.Data(expected, title="Expected")
    plot3 = Gnuplot.PlotItems.Data(actual, title="Acutal")


    return gp.plot(plot1, plot2, plot3)


    ----

    import Plotting2 #The name of my file...

    Plotting2.plot( [(2,3), (3,4)], [(4,5), (5,6)], [(1,3), (4,8)] )
     
    , Jun 12, 2008
    #1
    1. Advertising

  2. Fuzzyman Guest

    On Jun 12, 12:30 pm, wrote:
    > Hello. Was trying to create a simple plotting function. Wasnt working
    > however. If i write the same code without putting it inside a function
    > it works. :S. Could some1 tell me the problem? Heres the code:
    >
    > # File name Plotting2
    >
    > import Gnuplot
    >
    > def plot(original, expected, actual):
    >
    > if type (original) != type([]):
    > return False
    >
    > else:
    >
    > gp = Gnuplot.Gnuplot()
    > gp('set data style lines')
    >
    > # Make the plot items
    > plot1 = Gnuplot.PlotItems.Data(original, title="Original")
    > plot2 = Gnuplot.PlotItems.Data(expected, title="Expected")
    > plot3 = Gnuplot.PlotItems.Data(actual, title="Acutal")
    >
    > return gp.plot(plot1, plot2, plot3)
    >
    > ----
    >
    > import Plotting2 #The name of my file...
    >
    > Plotting2.plot( [(2,3), (3,4)], [(4,5), (5,6)], [(1,3), (4,8)] )


    I've no idea about the answer to your question (I don't know how the
    Gnuplot module works and I can't *see* anything obviously wrong with
    your code), but this line:

    if type (original) != type([])

    is better written:

    if not isinstance(original, list):

    Michael Foord
    http://www.ironpythoninaction.com/
     
    Fuzzyman, Jun 12, 2008
    #2
    1. Advertising

  3. Peter Otten Guest

    wrote:

    > Hello. Was trying to create a simple plotting function. Wasnt working
    > however. If i write the same code without putting it inside a function
    > it works. :S. Could some1 tell me the problem?


    Judging from the demo you have to keep a Gnuplot.Gnuplot instance alive. If
    you don't, the display window is immediately garbage-collected.

    > Heres the code:
    >
    >
    > # File name Plotting2
    >
    > import Gnuplot
    >
    > def plot(original, expected, actual):
    >
    >
    > if type (original) != type([]):
    > return False
    >
    > else:
    >
    > gp = Gnuplot.Gnuplot()
    > gp('set data style lines')
    >
    >
    > # Make the plot items
    > plot1 = Gnuplot.PlotItems.Data(original, title="Original")
    > plot2 = Gnuplot.PlotItems.Data(expected, title="Expected")
    > plot3 = Gnuplot.PlotItems.Data(actual, title="Acutal")
    >
    >

    gp.plot(plot1, plot2, plot3)
    return gp
    >
    >
    > ----
    >
    > import Plotting2 #The name of my file...
    >

    gp = Plotting2.plot( [(2,3), (3,4)], [(4,5), (5,6)], [(1,3), (4,8)] )
    raw_input()

    By the way, I recommend that you raise an Exception instead of returning a
    special value when plot() cannot deal with the arguments passed to it.

    Peter
     
    Peter Otten, Jun 12, 2008
    #3
  4. wrote:
    > Hello. Was trying to create a simple plotting function. Wasnt working
    > however. If i write the same code without putting it inside a function
    > it works. :S. Could some1 tell me the problem? Heres the code:
    >
    >
    > # File name Plotting2
    >
    > import Gnuplot
    >
    > def plot(original, expected, actual):
    >
    >
    > if type (original) != type([]):
    > return False
    >
    > else:
    >
    > gp = Gnuplot.Gnuplot()


    Replace this by (from
    http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module
    )
    gp = Gnuplot.Gnuplot(persist=1)
    > gp('set data style lines')
    >
    >
    > # Make the plot items
    > plot1 = Gnuplot.PlotItems.Data(original, title="Original")
    > plot2 = Gnuplot.PlotItems.Data(expected, title="Expected")
    > plot3 = Gnuplot.PlotItems.Data(actual, title="Acutal")
    >
    >
    > return gp.plot(plot1, plot2, plot3)
    >
    >
    > ----
    >
    > import Plotting2 #The name of my file...
    >
    > Plotting2.plot( [(2,3), (3,4)], [(4,5), (5,6)], [(1,3), (4,8)] )



    --
    Helmut Jarausch

    Lehrstuhl fuer Numerische Mathematik
    RWTH - Aachen University
    D 52056 Aachen, Germany
     
    Helmut Jarausch, Jun 13, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. morc
    Replies:
    2
    Views:
    695
    IchBin
    Feb 23, 2006
  2. Replies:
    4
    Views:
    951
    Grant Edwards
    Sep 6, 2005
  3. Replies:
    1
    Views:
    254
  4. Rob Garrett

    Plotting multiple datasets with gnuplot

    Rob Garrett, Oct 9, 2009, in forum: Python
    Replies:
    3
    Views:
    2,460
  5. ryan
    Replies:
    1
    Views:
    115
    Mumia W.
    May 30, 2007
Loading...

Share This Page