Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 29th, 2012, 09:27 AM
giacomo84 giacomo84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 13 giacomo84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old November 29th, 2012, 11:08 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
filename = 'best.dat'
k = 32
goodLabel = '%s %d'%(filename,k)


read about strings
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old November 30th, 2012, 09:29 AM
giacomo84 giacomo84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 13 giacomo84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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,
Code:
ylim(-3.0,3.0)
again it gives me the same error. And again, I can perfectly use thise comand outside the script...

Reply With Quote
  #4  
Old November 30th, 2012, 10:38 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 412 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 7 h 14 m 58 sec
Reputation Power: 65
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)

Reply With Quote
  #5  
Old November 30th, 2012, 10:44 AM
giacomo84 giacomo84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 13 giacomo84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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"...

Reply With Quote
  #6  
Old November 30th, 2012, 01:08 PM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 412 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 7 h 14 m 58 sec
Reputation Power: 65
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.

Reply With Quote
  #7  
Old December 3rd, 2012, 03:31 AM
giacomo84 giacomo84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 13 giacomo84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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'.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to use an index in the figure name and in legend

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap