The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
How to use an index in the figure name and in legend
Discuss How to use an index in the figure name and in legend in the Python Programming forum on Dev Shed. How to use an index in the figure name and in legend Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 29th, 2012, 09:27 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 3 h 21 m 37 sec
Reputation 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!
|

November 29th, 2012, 11:08 AM
|
 |
Contributing User
|
|
|
|
__________________
[code] Code tags[/code] are essential for python code!
|

November 30th, 2012, 09:29 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 3 h 21 m 37 sec
Reputation 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, again it gives me the same error. And again, I can perfectly use thise comand outside the script...
|

November 30th, 2012, 10:38 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | 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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

November 30th, 2012, 10:44 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 3 h 21 m 37 sec
Reputation 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"...
|

November 30th, 2012, 01:08 PM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | 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.
Quote: | 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.
|

December 3rd, 2012, 03:31 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 3 h 21 m 37 sec
Reputation 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'.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|