I am new in Python, I was working on that problem, but I faced with many errors. Firstly, I have 3 excel files, I need to find the highest 3 values from the each excel. Then, I need to take mean value of the each 3 highest values. After that, I need to plot the mean values, name of the excels must be at Y-axis, and mean values must be at X-axis.
I had the maximum values and mean values without any error, but I need to contact them each other and get a graph. I wrote that codes, but it did not work. How can I fix the errors? Thank you.
import numpy as np
import xlrd
book = xlrd.open_workbook("inc.xlsx")
sheet = book.sheet_by_index(0)
def inc_follow_up():
col = 1
return max(sheet.col_values(col, start_rowx=2, end_rowx=1441)[:3])
def inc_avg():
sum(inc_follow_up()) / len(inc_follow_up())
import xlrd
book = xlrd.open_workbook('mok.xlsx')
sheet = book.sheet_by_index(0)
def mok_follow_up():
col = 1
return max(sheet.col_values(col, start_rowx=2, end_rowx=1441)[:3])
def mok_avg():
sum(mok_follow_up()) / len(mok_follow_up())
import xlrd
book = xlrd.open_workbook('sok.xlsx')
sheet = book.sheet_by_index(0)
def sok_follow_up():
col = 1
return max(sheet.col_values(col, start_rowx=2, end_rowx=1441)[:3])
def sok_avg():
sum(sok_follow_up()) / len(sok_follow_up())
plt.plot(inc_avg["Significant wave height (cm)"],inc, color='green', label="Inc")
plt.plot(mok_avg["Significant wave height (cm)"], mok, color='green', label="Mok")
plt.plot(sok_avg["Significant wave height (cm)"], sok, color='green', label="Sok")
plt.title("Free")
plt.ylabel("Significant wave height (cm)")
plt.xlabel("inc","mok","sok")
plt.legend()
plt.show()
The Problem:
So i think i now understand what you are trying to do and i will try to resolve your little problem. BUT as I see it your little code excerpt is full of minor and major flaws and you will need to familiarize yourself more with the basics of python or you will hit problems at every turn. But more of that later... Here's my "solution" (hope it works):
First put all of your import statements at the top of your script (except for some exceptions) and actually import matplotlib as plt:
import numpy as np
import matplotlib.pyplot as plt
import xlrd
Then you seem to want a function that returns you a number of the largest values from some list. Let's do this:
def nLargest(ls, n):
'''return the n largest entries in ls'''
return list(reversed(sorted(ls)[-n:]))
This function above takes two arguments: the list and the number of largest values that should be returned. It first sorts the data with "sorted(ls)" (lowest to highest) and takes the last "n" elements with "[-n:]". The list is then reversed (highest to lowest) and returned.
Now just define some values, load the data, and calculate your desired values:
sheetIndex = 0
col = 1
start = 2
end = 1441
book = xlrd.open_workbook("inc.xlsx")
sheet = book.sheet_by_index(sheetIndex)
incData = sheet.col_values(col, start_rowx=start, end_rowx=end)
book = xlrd.open_workbook('mok.xlsx')
sheet = book.sheet_by_index(sheetIndex)
mokData = sheet.col_values(col, start_rowx=start, end_rowx=end)
book = xlrd.open_workbook('sok.xlsx')
sheet = book.sheet_by_index(sheetIndex)
sokData = sheet.col_values(col, start_rowx=start, end_rowx=end)
averages = [np.average(nLargest(incData, 3)), # use numpy for averaging
np.average(nLargest(mokData, 3)),
np.average(nLargest(sokData, 3))]
Finally to plot it the way you want it to:
vals = [1., 2., 3.]
plt.plot(vals, averages)
plt.xticks(vals, ["inc","mok","sok"])
plt.title("Free")
plt.ylabel("Significant wave height (cm)")
plt.xlabel("source files")
plt.show()
All the other problems
Now i want to spend some time looking over your original code and i will just criticize wherever i feel like it:
def follow_up():
col = 1
return max(sheet.col_values(col, start_rowx=2, end_rowx=1441)[:3])
To be honest this function ( and most of the others ) made me cringe so much... you shouldn't alter or really access global variables inside a function. A function call should in itself contain most of the data the function will need ( or at least all information to get the data ). You wrote two times three functions that essentially do the same thing on a different data set. While this is what a function is for you should just write one function and use it for all the data, a bit like this:
def follow_up(sheet, col, start, end, n):
'''This function will still not work but at least it's a function...'''
return max(sheet.col_values(col, start_rowx=start, end_rowx=end)[:n])
Then you are doing the "import xlrd" multiple times. No need for that. Your plotting also needs some second or third glances. Be sure to read the reference manuals of the libraries you are using at least a tiny little bit before actually using them.
Sources:
https://xlrd.readthedocs.io/en/latest/api.html
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html
There is plenty of information and examples on those sites.