how to make my horizontal histogram to vertical - python

this is a part of my program which generates * patterns from 4 user inputs
total= progresscount + trailercount + retrivercount + excludecount
print("....................................................")
print("Histogram")
print("Progress",progresscount,":" , "*" * progresscount)
print("Trailer",trailercount,":" , "*" * trailercount)
print("Retriver",retrivercount,":" , "*" * retrivercount)
print("Excluded",excludecount ,":" , "*" * excludecount )
print("....................................................")
if I run my whole program and enter 2 3 1 and 2 for each input requests, output looks like this
....................................................
Histogram
Progress 2 : **
Trailer 3 : ***
Retriver 1 : *
Excluded 2 : **
....................................................
and I want to make a vertical version like this
....................................................
Progress Trailing Retriever Excluded
* * * *
* * *
*
....................................................
help me to figure this out?

You can create a custom function like below:
def display_vhist(data):
cell_len = max([len(k) for k in data]) + 2
max_val = max(data.values())
# display header
print(*[h.center(cell_len) for h in data.keys()], sep='')
# display histogram
for i in range(1, max_val+1):
for v in data.values():
c = '*' if i <= v else ' '
print(c.center(cell_len), end='')
print()
Usage:
data = {'Progress': 2, 'Trailer': 3, 'Retriver': 1, 'Excluded': 1}
display_vhist(data)
# Output
Progress Trailer Retriver Excluded
* * * *
* *
*

Related

evaluation of polynomials of order 15 gives unexpected results (OpenCV Coordinates Output Python)

I've created a formula with polynomial regression to calculate a drag point from an input. I tested the formula in a separate file with the input, and the output appears to be working as intended. However, in the actual file, it seems to be outputting tremendously incorrect numbers. My first thought was that the output style for OpenCV was in some odd format for the numbers, but when I printed the values they were just fine. It appears to work just fine with smaller formulas, but gives wild values for my created formula. Any help would be appreciated. Below are the formulae for calculating the x and y with a test input and a correct output, vs what I'm getting in the actual file. Thanks.
def triang_x(x1):
x2 = (-6.8808613455609384e+005) + (3.2312735687925651e+003 * x1)
+ (-6.0019455279289815e+000 * x1**2) + (1.0786025430222985e-002 * x1**3)
+ (-2.8031333306353576e-005 * x1**4) + (4.7125204049478773e-008 * x1**5)
+ (-3.8733353649642116e-011 * x1**6) + (1.4733899082497896e-014 * x1**7)
+ (-5.1033986030610612e-018 * x1**8) + (2.7445807881521161e-021 * x1**9)
+ (1.4039325861603808e-024 * x1**10) + (-7.8303365296494140e-027 * x1**11)
+ (2.0700235162417034e-029 * x1**12) + (-2.5633522287710555e-032 * x1**13)
+ (1.4236656075804622e-035 * x1**14) + (-2.9388878284829885e-039 * x1**15)
return x2
def triang_y(y1):
y2 = ((2.9536073194970668e+004) + (-7.2584981060026985e+002 * y1)
+ (5.9991721893954519e+000 * y1**2) + (-1.4273839368311947e-002 * y1**3)
+ (-6.1205911580247642e-005 * y1**4) + (4.1603526512587676e-007 * y1**5)
+ (-6.9546008738218303e-010 * y1**6) + (-1.1072665851528698e-013 * y1**7)
+ (-6.4446469064614884e-016 * y1**8) + (8.0190196135612936e-018 * y1**9)
+ (-8.8768139841444641e-021 * y1**10) + (-1.3685149110264805e-023 * y1**11)
+ (1.3193560897991867e-026 * y1**12) + (5.4138560249698032e-029 * y1**13)
+ (-9.5141032455036651e-032 * y1**14) + (4.4497796299711634e-035 * y1**15))
return y2
# Test Values are x:1116 y:398
#Correct Output is x:900.892612375319 y:889.0486684303542
#Output received in main is 10415680.385044796 -167144.0485716732
The way your question is posed currently, I can't make heads or tails of it.
I can tell you that your first function has an error: you're trying to assign a sum of 16 summands but when you break that line, everything following will not be part of that sum. You should have gotten an IndentationError... Unless the code in your question is indented differently from what you actually ran on your own computer. If you indented that just right, you would simply have a bunch of 2-summand additions going off into nowhere because it's perfectly legal in python to have an expression whose value you don't assign to anything. To fix that, put parentheses ( and ) around the entire expression on the right-hand side of the assignment.
Your second function looks okay and appears to work as you want it. I can't reproduce the "wrong values" you state in your question.
Beyond that... instead of writing such a huge python expression, simply use numpy to express the coefficients and the evaluation of the polynomial:
import numpy as np
coeffs_x = np.float64([
-6.8808613455609384e+005, # 0
+3.2312735687925651e+003, # 1
-6.0019455279289815e+000, # 2
+1.0786025430222985e-002, # 3
-2.8031333306353576e-005, # 4
+4.7125204049478773e-008, # 5
-3.8733353649642116e-011, # 6
+1.4733899082497896e-014, # 7
-5.1033986030610612e-018, # 8
+2.7445807881521161e-021, # 9
+1.4039325861603808e-024, # 10
-7.8303365296494140e-027, # 11
+2.0700235162417034e-029, # 12
-2.5633522287710555e-032, # 13
+1.4236656075804622e-035, # 14
-2.9388878284829885e-039, # 15
])
triang_x = np.polynomial.Polynomial(coeffs_x)
coeffs_y = np.float64([
+2.9536073194970668e+004, # 0
-7.2584981060026985e+002, # 1
+5.9991721893954519e+000, # 2
-1.4273839368311947e-002, # 3
-6.1205911580247642e-005, # 4
+4.1603526512587676e-007, # 5
-6.9546008738218303e-010, # 6
-1.1072665851528698e-013, # 7
-6.4446469064614884e-016, # 8
+8.0190196135612936e-018, # 9
-8.8768139841444641e-021, # 10
-1.3685149110264805e-023, # 11
+1.3193560897991867e-026, # 12
+5.4138560249698032e-029, # 13
-9.5141032455036651e-032, # 14
+4.4497796299711634e-035, # 15
])
triang_y = np.polynomial.Polynomial(coeffs_y)
print(triang_x(1116), triang_y(398))
# => 900.8926123741549 889.0486684304415

Simple python histogram

result1 = ["Progress","Progress(MT)","ModuleRT","Exclude"]
result2 = [3,4,3,5]
def histogram (list1,list2):
for i in range (len(list1)):
print(list1[i])
for j in range (list2[i]):
print("","*")
histogram(result1,result2)
I'm trying to get the output like this, but I can't seem to get that.
Progress Progress(MT) ModuleRT Excluded
* * * *
*
Using center(), you can build the columns with the width corresponding to their respective title size. The histogram itself will need as many lines as the maximum value in result2. Each column should only print a star if the line index is less than the corresponding value.
result1 = ["Progress","Progress(MT)","ModuleRT","Exclude"]
result2 = [3,4,3,5]
print(*result1)
for i in range(max(result2)):
print(*( " *"[i<r].center(len(t)) for t,r in zip(result1,result2)))
Progress Progress(MT) ModuleRT Exclude
* * * *
* * * *
* * * *
* *
*
The histogram would look better if the columns were above the titles. You can do this by simply reversing the order of the line index:
for i in reversed(range(max(result2))):
print(*( " *"[i<r].center(len(t)) for t,r in zip(result1,result2)))
print(*result1)
*
* *
* * * *
* * * *
* * * *
Progress Progress(MT) ModuleRT Exclude
Converted to a function:
def histogram(titles,values):
print(*titles)
for i in range(max(values)):
print(*( " *"[i<v].center(len(t)) for t,v in zip(titles,values)))
Hello Alex and welcome on the Stackoverflow
I assume that, you want to get something like this below:
Progress * * *
Progress(MT) * * * *
ModuleRT * * *
Exclude * * * * *
Then, you have to modify your code, so that the print method does not add a new line after every * printed. To do so, you use end argument and set it to an empty character, like this: print("some string", end='')
So your new code would be:
result1 = ["Progress","Progress(MT)","ModuleRT","Exclude"]
result2 = [3,4,3,5]
def histogram (list1,list2):
for i in range (len(list1)):
print(list1[i], end='') # here is the end added
for j in range (list2[i]):
print("","*", end='') # here is the end added
histogram(result1,result2)
Nevertheless, it won't end-up in something like this:
Progress * * *Progress(MT) * * * *ModuleRT * * *Exclude * * * * *
The thing is, that there's no new line character after first outer for loop iteration. So you print a new line with an empty print at the end of the outer loop like this print("").
So finally your code would look like this:
result1 = ["Progress","Progress(MT)","ModuleRT","Exclude"]
result2 = [3,4,3,5]
def histogram (list1,list2):
for i in range (len(list1)):
print(list1[i], end='')
for j in range (list2[i]):
print("","*", end='')
print("") # here is the print
histogram(result1,result2)

Access list items -python

result1 = [prg_count,prg_mt_count,dnt_prg_count,excld_count]
total_students = prg_count + prg_mt_count + dnt_prg_count + excld_count
result2 = ["Progress", "Progress(MT)", "Do not Progress(MT)", "Exclude"]
def histogram (list1):
for i in range (len(list1)):
for j in range (list1[i]):
print("*",end="")
print("")
print('_' * 30)
print("Horizontal Histogram")
histogram (result1)
print(total_students, "outcomes in total")
print('_' * 30)
I want to print the items in the result2 list, one by one with their histogram value.
Histogram value changes according to user inputs and they're assigned to result1 list items.
what I'm trying to get;
Progress : *
Progress(MT) : *
Do not Progress(MT) : **
Excluded 1 : *
Did some improvements to your code, instead of loop for * printing I used '*' * count, same like you did for horizontal line '_' * 30, also printed names of histogram keys as you wished in your Question, plus used string's method .ljust(21) which adds extra spaces to align several strings to the same width.
Also as an example I showed histogram values 1, 5, 2, 3, see my Output after code below.
Try it online!
prg_count, prg_mt_count, dnt_prg_count, excld_count = 1, 5, 2, 3
result1 = [prg_count,prg_mt_count,dnt_prg_count,excld_count]
total_students = prg_count + prg_mt_count + dnt_prg_count + excld_count
result2 = ["Progress", "Progress(MT)", "Do not Progress(MT)", "Exclude"]
def histogram (list1, list2):
for a, b in zip(list1, list2):
print(b.ljust(21) + '*' * a)
print('=' * 30)
print("Horizontal Histogram")
print('-' * 30)
histogram(result1, result2)
print('-' * 30)
print(total_students, "outcomes in total")
print('=' * 30)
Output:
==============================
Horizontal Histogram
------------------------------
Progress *
Progress(MT) *****
Do not Progress(MT) **
Exclude ***
------------------------------
11 outcomes in total
==============================
Your question is not clear, but I think I know what you are saying. If you want to print the titles to the values (as I think your desired output says) you need to pass your titles in.
result1 = [5, 4, 2, 3]
total_students = sum(result1)
result2 = ["Progress","Progress(MT)","Do not Progress(MT)","Exclude"]
def histogram(result2, result1):
for i in range(len(result2)):
print(result2[i], end=" ")
for j in range(result1[i]):
print('*', end="")
print('')
print('_' * 30)
print("Horizontal Histogram")
histogram (result2, result1)
print(total_students, "outcomes in total")
print('_' * 30)
If you want to make it more succinct you can do this instead.
output = zip(result2, result1)
def histogram(list1):
for title, value in list1:
print(f"{title:20} {''.join(['*' for i in range(value)])}\n")
histogram(output)

Is it possible to put the results of these for loops into one line?

I'm trying to create a vertical histogram that takes user input and displays the results in a vertical histogram.
I think I've figured out how to do it using for loops and f string format, but I don't know how, or if it's even possible, to get the outcomes on the same line so that it prints on one row.
Here is my code below:
def vert_histogram():
print('----------------------------------------------------------------------')
print('Vertical Histogram')
choices = 'Progress | Trailer | Retriever| Exclude'
print(choices)
star = '*'
no_star = ' '
for i in range(progress):
print(f'{star:>4}')
for i in range(trailer):
print(f'{star:>15}')
for i in range(retriever):
print(f'{star:>26}')
for i in range(exclude):
print(f'{star:>36}')
vert_histogram()
At the moment, my current output looks like this:
Vertical Histogram
Progress | Trailer | Retriever| Exclude
*
*
*
*
*
*
*
*
*
*
You could do something like this.
progress = 4
trailer = 2
retriever = 3
exclude = 1
stars = [progress,trailer,retriever,exclude]
maxval = max(stars)
print('----------------------------------------------------------------------')
print('Vertical Histogram')
choices = 'Progress | Trailer | Retriever | Exclude'
print(choices)
for i in range (maxval):
a = ['*' if i < star else ' ' for star in stars]
print (f'{a[0]:>4}{a[1]:>11}{a[2]:>11}{a[3]:>11}')
This will result in the following output:
----------------------------------------------------------------------
Vertical Histogram
Progress | Trailer | Retriever | Exclude
* * * *
* * *
* *
*

Shortening a for loop which pads first and last value of several lists

I have 3 lists (EIRP,data3,data15) and I'm looking for a way to shorten this code:
ws=3
for i in range(ws):
EIRP.insert(0, EIRP[0])
EIRP.append(EIRP[-1])
data3.insert(0,data3[0])
data3.append(data3[-1])
data15.insert(0,data15[0])
data15.append(data15[-1])
EIRP[0:0] = EIRP[:1] * 3
EIRP.extend(EIRP[-1:] * 3)
data3[0:0] = data3[:1] * 3
data3.extend(data3[-1:] * 3)
data15[0:0] = data15[:1] * 3
data15.extend(data15[-1:] * 3)
even shorter
EIRP = EIRP[:1] * 3 + EIRP[:] + EIRP[-1:] * 3
data3 = data3[:1] * 3 + data3[:] + data3[-1:] * 3
data15 = data15[:1] * 3 + data15[:] + data15[-1:] * 3
li = [EIRP,data3,data15]
ws = 3
for i in range(ws):
for j in li:
j.insert(0, j[0])
j.append(j[-1])
I hope that this is what you want

Categories