#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    How to use an index in the figure name and in legend


    Hi. I want tu use current index as part of the name i'm using to save a plot, and i want that this name appears in legend. I give an example:

    i want to make a while loop, from i=0 to 4, to represent the same number of x and y vectors;
    when i declare the label of the plot, i want give a name and the number associated to i, eg: my label must be "cosine 2", where "cosine" is the usual declaration of name (label="cosine ") and 2 is the current value for "i".

    The same kind of thing i have to do with the file name, when i save the plot; eg: my figure must have the name "picture 2", where "picture" is the usual declaration of name (savefig "picture.png") and 2 is the current value for "i". But, in this case, I also need to add the file extension after the number.

    How i can use, so, the index?

    thanks a lot!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    filename = 'best.dat'
    k = 32
    goodLabel = '%s %d'%(filename,k)


    read about strings
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0
    well, i read yet everything, but maybe there is another problem, because: when i give the code directly, there are no problems. I can make also a while loop, ok perfect.

    i use the same code in the script, and it doesn't work.

    i try to be as much clear as i can:

    if i make a very simple script, in which i work with two arrays, x and y, and i want to put them in the plot and i want the legend i do the following script, with the same identical code i use successfully directly outside the script:

    Code:
    #prova.py  
    from numpy import * 
    #from matplotlib.pyplot as pl 
    from matplotlib.pyplot import * 
    from pylab import * 
    import os    
    x=arange(12) 
    y=sin(x)  
    figure()  
    plot(x,y,color='blue',label="Velocità" ) 
    legend(loc='upper right')       
    plot([3,3],[0,y.max()],color='red',linestyle="--")     
    savefig("prova.png")
    but, making the script, i receive always an error:
    Code:
    >>> import prova
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "prova.py", line 16
    SyntaxError: Non-ASCII character '\xc3' in file prova.py on line 16, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
    >>>
    if I don't use the label and legend comands, everything works, if i use,eg,
    Code:
    ylim(-3.0,3.0)
    again it gives me the same error. And again, I can perfectly use thise comand outside the script...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    434
    Rep Power
    67
    Originally Posted by giacomo84
    Code:
    plot(x,y,color='blue',label="Velocità" )
    Python 2.x’s handling of character encodings was seriously flawed. Basically you should always specify the encoding in the beginning of the file like this:

    Code:
    # prova.py
    # -*- coding: UTF-8 -*-
    (Or whatever your encoding is.) Even this is not enough for most of the time. You might try adding “u” before literal strings containing non-ASCII characters so as to mark them as Unicode, like:

    Code:
    plot(x,y,color='blue',label=u"Velocità")
    Sometimes you are forced to use str.decode() and str.encode() methods heavily.

    Python 3 solves all this, but many libraries as still not ported to it.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0
    OMG this is ... nice... :S

    why the devil i don't need this specification when i make this code in the python window? (how do you call it, GUI?)

    anyway, i tried also to delete the label, and i set the limits on y axis as i said before, and the same error was declared... and there were no strange "characters"...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    434
    Rep Power
    67
    Originally Posted by giacomo84
    why the devil i don't need this specification when i make this code in the python window? (how do you call it, GUI?)
    I guess IDLE (or whatever GUI you are using) sets the encoding itself.

    anyway, i tried also to delete the label, and i set the limits on y axis as i said before, and the same error was declared... and there were no strange "characters"...
    If it’s the same error, just look at the line number in the error message. The offending character (like “à” in the example) should be there.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0
    I used the command string you told me, it works, but I must anyway delete the "à" thet it doesn't understand. Anyway, it works

    I could also use the index in the figure label, but, when using "savefig", I still can't give a name and an index. I didn't found an example or an explanation to help me...

    I saw that there is another small problem, because, if the index is floating point, the label is always integer; example the index is i=2,34 and I want the name is 'v=2,34', I obtain 'v=2'.

IMN logo majestic logo threadwatch logo seochat tools logo