![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Location: Indianapolis
Posts: 2
Rep Power: 0
![]() |
I know....i know.....sounds stupid...but just look and see if anyone can give me a hand.
I am creating a script that needs to display numbers at the top of a graph. The problem is...the numbers are out to around 9 decimal places...I need them at 1. I have tried the round command, but it doesnt work. The following is the code...the bold areas being the problem areas. (and yes...'E'...is a list of numbers.--->E=[.......]) Python will not let me round E...giving me the error message---> typeerror: a float is required. If anyone can help...you have my thanks. (The {value} input refers to the data= section of code) ------------------------------------------------------------- def sort_by_value(d):
""" Returns the keys of dictionary d sorted by their values """
items=d.items()
backitems=[ [v[1],v[0]] for v in items]
backitems.sort()
return [ backitems[i][1] for i in range(0,len(backitems))]
SBV=sort_by_value
A=SBV(E)
B=sorted(E.values())
#!/usr/bin/python
from pychartdir import *
#The data for the bar chart
data = B
#The labels for the bar chart
labels = ["BMS","ST","FG","CV","LN","CMS","LC","BP","MEC","ABE","HH","IC","MCC","OE","SS","FCV","WR"]
#The colors for the bar chart
colors=[0x000099,0x003300,0x003300,0x003300,0x990000,0x000099,0x990000,0x003300,0x003300,0x003300,0x003300,0x003300,0x990000,0x003300,0x003300,0x000099,0x003300]
#Create a XYChart object of size 260 x 220 pixels. Use golden background color.
#Use a 2 pixel 3D border.
c = XYChart(780, 660, goldColor(), -1, 2)
#Add a title box using 10 point Arial Bold font. Set the background color to
#metallic blue (9999FF) Use a 1 pixel 3D border.
c.addTitle("Educational Adequacy", "arialbd.ttf", 10).setBackground(metalColor(
0x9999ff), -1, 1)
#Set the plotarea at (40, 40) and of 200 x 150 pixels in size
c.setPlotArea(120, 120, 600, 450)
#Add a multi-color bar chart layer using the given data and colors. Use a 1
#pixel 3D border for the bars.
#Set the labels on the x axis.
c.xAxis().setLabels(labels)
layer = c.addBarLayer(data, c.gradientColor(120, 0, 717, 0, 0xff0000, 0x0000ff))
#Set the bar gap to 10%
layer.setBarGap(0.1)
#Use the format " xxx " as the bar label
layer.setAggregateLabelFormat("{value}")
#Set the bar label font to 10 pts Times Bold Italic/dark red (0x663300)
layer.setAggregateLabelStyle("timesbi.ttf", 10, 0x663300)
#output the chart
c.makeChart("Ecompare.jpg")Last edited by sojo; Jun 23rd, 2005 at 9:48 AM. Reason: problem solved |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
My Python is broken so I can't do it for you.
But if you want to set something to only one decimal place, you can convert to string, and use [] to only select up to the first decimal place. Or you could multiply by 10. Convert to integer to kill the rest of the decimals, then convert back to float by dividing by 10. There are a bunch of other simple methods too... |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Feb 2005
Posts: 54
Rep Power: 4
![]() |
Could you explain why round() isn't working? Works fine for me:
E = [1.1111111111, 2.222222222, 3.33333333, 100.5555555, -341.99]
E = [round(i, 1) for i in E]
for num in E:
print num1.1 2.2 3.3 100.6 -342.0 |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Location: Indianapolis
Posts: 2
Rep Power: 0
![]() |
Thanks for your help Sane, I worked it out by multiplying by 10
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|