I have a list of values that I am plotting and every time I loop through the list, I create a plot. However, the plots overwrite every time it goes through the loop. This is what I tried for far that did not work.
myPath = "//my/absolute/path"
for i in list_val:
i.plot('var1', 'var2')
plt.savefig(os.path.join(myPath,''.join("figure{y}.png".format(y = i))))
plt.show()
However, when I tried the following, it overwrites the images(which I knew it would happen),
myPath = "//my/absolute/path"
for i in list_val:
i.plot('var1', 'var2')
plt.savefig(os.path.join(myPath,''.join("figure.png")))
plt.show()
How can I modify my first snippet above to avoid overwriting images?
How about
myPath = "//my/absolute/path"
for index,df in enumerate(list_val):
df.plot('var1', 'var2')
plt.savefig(os.path.join(myPath,''.join("figure{y}.png".format(y = index))))
plt.show()
Try
myPath = "//my/absolute/path"
for i in range(1, len(list_val)):
list_val[i].plot('var1', 'var2')
plt.savefig(os.path.join(myPath,''.join("figure{y}.png".format(y = i+1))))
plt.show()
Related
This seems like it should be very simple but am not sure the proper syntax in Python. To streamline my code I want a while loop (or for loop if better) to cycle through 9 datasets and use the counter to call each file out using the counter as a way to call on correct file.
I would like to use the "i" variable within the while loop so that for each file with sequential names I can get the average of 2 arrays, the max-min of this delta, and the max-min of another array.
Example code of what I am trying to do but the avg(i) and calling out temp(i) in loop does not seem proper. Thank you very much for any help and I will continue to look for solutions but am unsure how to best phrase this to search for them.
temp1 = pd.read_excel("/content/113VW.xlsx")
temp2 = pd.read_excel("/content/113W6.xlsx")
..-> temp9
i=1
while i<=9
avg(i) =np.mean(np.array([temp(i)['CC_H='],temp(i)['CC_V=']]),axis=0)
Delta(i)=(np.max(avg(i)))-(np.min(avg(i)))
deltaT(i)=(np.max(temp(i)['temperature='])-np.min(temp(i)['temperature=']))
i+= 1
EG: The slow method would be repeating code this for each file
avg1 =np.mean(np.array([temp1['CC_H='],temp1['CC_V=']]),axis=0)
Delta1=(np.max(avg1))-(np.min(avg1))
deltaT1=(np.max(temp1['temperature='])-np.min(temp1['temperature=']))
avg2 =np.mean(np.array([temp2['CC_H='],temp2['CC_V=']]),axis=0)
Delta2=(np.max(avg2))-(np.min(avg2))
deltaT2=(np.max(temp2['temperature='])-np.min(temp2['temperature=']))
......
Think of things in terms of lists.
temps = []
for name in ('113VW','113W6',...):
temps.append( pd.read_excel(f"/content/{name}.xlsx") )
avg = []
Delta = []
deltaT = []
for data in temps:
avg.append(np.mean(np.array([data['CC_H='],data['CC_V=']]),axis=0)
Delta.append(np.max(avg[-1]))-(np.min(avg[-1]))
deltaT.append((np.max(data['temperature='])-np.min(data['temperature=']))
You could just do your computations inside the first loop, if you don't need the dataframes after that point.
The way that I would tackle this problem would be to create a list of filenames, and then iterate through them to do the necessary calculations as per the following:
import pandas as pd
# Place the files to read into this list
files_to_read = ["/content/113VW.xlsx", "/content/113W6.xlsx"]
results = []
for i, filename in enumerate(files_to_read):
temp = pd.read_excel(filename)
avg_val =np.mean(np.array([temp(i)['CC_H='],temp['CC_V=']]),axis=0)
Delta=(np.max(avg_val))-(np.min(avg_val))
deltaT=(np.max(temp['temperature='])-np.min(temp['temperature=']))
results.append({"avg":avg_val, "Delta":Delta, "deltaT":deltaT})
# Create a dataframe to show the results
df = pd.DataFrame(results)
print(df)
I have included the enumerate feature to grab the index (or i) should you want to access it for anything, or include it in the results. For example, you could change the the results.append line to something like this:
results.append({"index":i, "Filename":filename, "avg":avg_val, "Delta":Delta, "deltaT":deltaT})
Not sure if I understood the question correctly. But if you want to read the files inside a loop using indexes (i variable), you can create a list to hold the contents of the excel files instead of using 9 different variables.
something like
files = []
files.append(pd.read_excel("/content/113VW.xlsx"))
files.append(pd.read_excel("/content/113W6.xlsx"))
...
then use the index variable to iterate over the list
i=1
while i<=9
avg(i) = np.mean(np.array([files[i]['CC_H='],files[i]['CC_V=']]),axis=0)
...
i+=1
P.S.: I am not a Pandas/NumPy expert, so you may have to adapt the code to your needs
x = [1 2 5 6 3 .....]
n = len(x)//34
i = 0
while i < n*34:
fig, axs = plt.subplots(2)
axs[0].plot(x[i:34+i],y[i:34+i],x_l[i:34+i],y_l[i:34+i]) #knee
axs[1].plot(x_a[i:34+i],y_a[i:34+i],color = 'red') #ank
axs[1].plot(x_l_a[i:34+i],y_l_a[i:34+i],color = 'green')
axs[0].axis('off')
axs[1].axis('off')
plt.savefig('test')
i = i + 17
The code above will only save the last plot in my file, but I would like to generate and save the multiple plots for different values of i.
Currently, you are overwriting the same "test" file on every iteration; as such, when your program completes, you are left with only the last figure saved to disk. If you want to save multiple files, you need to use a different filename on every iteration in your call to savefig.
For example, you may use the iteration variable i and change your call to:
plt.savefig("test{}".format(i))
I have a question concering my code for a data evaluation of an experiment:
in a first for loop I am opening file after file which I want to analyze. inside this for loop, so inside one file, I want to create a second for loop to evaluate the some specific paramters for evaluation. when I do it just for one file, the parameters are correct, but when I loop over all files, it looks like in the second for loop these paramteres are summed up. so the normal value should be in the range of ar= 0.0001, for one file perfectly working. when I loop over the files I then get 0.0001 for the first one, 0.0002 for the second, 0.0003 for the thrid, etc.
update:
ok so here is the whole part of the code. for each file I want after fitting the data to get the sum over the difference between two datapoints in the first column (x[j]) inside the file multiplicated by the coressponding value in the second columnn (y[j]) (each file has two columns with a length of 720 datapoints) and the result of this should then be stored in AR for each file.
def sum_list(l):
sum = 0
for k in l:
sum += k
return sum
INV= []
DIFFS= []
AR= []
for i in range(0,len(fnames)):
data= np.loadtxt(fnames[i])
x= data[:,0]
y=data[:,1]
gmod=lm.Model(linmod)
result= gmod.fit(y, x=x, p=0.003, bg=0.001)
plt.plot(x, y)
plt.plot(x, result.best_fit, 'r-')
plt.show()
print result.best_values['bg']
print result.best_values['p']
p= result.best_values['p']
bg1= result.best_values['bg']
for j in range(0, 719):
diffs = ((x[j+1]- x[j])*y[j])
DIFFS.append(diffs)
ar= sum_list(DIFFS)
AR.append(ar)
inr= (x[0]-bg1)*(y[0]**3)/3 + ar
INV[i]= inr
If you are working with files (e.g opening them), I suggest to use os module, maybe a construct like this will help you to avoid the nested for loop:
for root,dirs,files in os.walk(os.getcwd()):
for i in files:
with open(os.path.join(root,i)) as f:
#do your summation
I created a simple csv file with numbers that approach pi and I would like to create and store the output as a png. I have a very simple csv, each tow contains the number I want to graph and
import pandas as pd
import csv
import matplotlib.pyplot as plt
from decimal import Decimal
def create_png():
df = pd.read_csv('sticks.csv', names=["xstk", "stk"])
sumdf = df.sum(0)
num1 = sumdf['xstk']
num2 = sumdf['stk']
total = num1 + num2
aproxpi = [(2*float(total))/num1]
with open('aproxpi.csv', 'a') as pifile:
piwriter = csv.writer(pifile, delimiter= ' ')
piwriter.writerow(aproxpi)
Piplot = pd.read_csv('aproxpi.csv', names=['~Pi'])
#Piplot.groupby('~Pi')
Piplot.plot(title='The Buffon Needle Experiment')
if __name__ == "__main__":
create_png()
When I run this code nothing happens. If I use the show method on the AxesSubPlot I raise an exception. How can this be accomplished?
You need to call plt.show() to actually see the plot.
This code seems very incomplete - is there more you can give us?
It may be that Piplot.plot needs to have x and y specified, instead of simply a title. I believe that you need to create a new plot object and pass the data into it, rather than calling data.plot() as you are now. See the documentation.
Additionally, taking a look at this question may help.
i have this code for creating a series of image from a series of matrixes, and in each image i want to add a specific text. this is my typical code :
ax = axes([0,0,1,1])
for i in range(0,5):
text(1,1,str(i))
ax.imshow(a[:,:,i],origin='lower')
savefig(str("%04d" % int(i))+'.png',format="png")
del ax.texts[-1]
but the problem is that as the number of iteration increases, the speed decease and it becomes so so slow. It seems that there is something wrong with opening a lot of windows in background.
Any suggestion?
Instead of creating a new image and text objects every loop reuse the objects.
ax = axes([0,0,1,1])
t = text(1,1,str(0))
img = ax.imshow(a[:,:,0],origin='lower')
for i in range(0,5):
t.set_text(str(i)
img.set_data(a[:,:,i])
savefig(str("%04d" % int(i))+'.png',format="png")
also see
Visualization of 3D-numpy-array frame by frame
I just added this single line at the end of the loop and it works fine now. It was simply the problem of accumulating previuosly opened figures in the memory.
ax = axes([0,0,1,1])
for i in range(0,5):
text(1,1,str(i))
ax.imshow(a[:,:,i],origin='lower')
savefig(str("%04d" % int(i))+'.png',format="png")
del ax.texts[-1]
close(gcf())