The current plot generates only one line but I want multiple lines in the same plot iterating through variables in data frame df2
Now I am stuck as the return of a function is data frame and how can I store it in 'plot3'
variable?
How can I plot wp(i)
, tempp(i)
{5 sets of date} and etcs in same plots.
Representative plot:
import pandas as pd
import random
def function(ConstantA, ConstantB, tst, temp, dtube):
# some function
randomlist = []
for i in range(0, 5):
n = random.randint(1, 30)
wp= ConstantA * randomlist
tempp=ConstantB * randomlist
ycp = tst * randomlist
yap = temp * randomlist
pp = dtube * randomlist
all = pd.DataFrame({'wp': wp, 'tempp': tempp, 'ycp': ycp, 'yap': yap, 'pp': pp, })
return all
ConstantA = [0.01545, 0.6, 0.6, 0.6, 0.6];
ConstantB = [0.6885e-6, 0, 0, 0, 0];
tst = [523, 529, 531, 527, 533];
temp = [523, 524, 524, 524, 524];
dtube = [0.041, 0.041, 0.041, 0.041, 0.041, 0.041];
df2 = pd.DataFrame(list(zip(ConstantA, ConstantB, tst, temp, dtube)),
columns=['ConstantA', 'ConstantB', 'tst', 'temp', 'dtube'])
for index, row in df2.iterrows():
plot3 = function(row['ConstantA'], row['ConstantB'], row['tst'], row['temp'], row['dtube'])
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Cooled;Tst=523')
ax1.plot(wp, tempp)
ax1.set_ylabel('T(K)')
ax2.plot(wp, ycp)
ax2.set_ylabel('y(C%)')
ax3.plot(wp, yap)
ax3.set_ylabel('yA ()')
ax3.set_xlabel('w (1000 ka)')
ax4.plot(wp, pp)
ax4.set_ylabel('E (bar)')
ax4.set_xlabel('w (1000 kg)')
plt.tight_layout() # Automatic adjustment of pyplot so ylabels dont overlap
plt.subplots_adjust(top=0.9) # Adjust plots to put space beween title and subplot
plt.show()
CodePudding user response:
I see few problems.
You should create fig, ax1, ax2, ax3, ax4
before for
-loop and later plot plot3
inside for
-loop. This way you will draw on the same plots.
You should use numpy
to generate randomlist
because you can't do ConstantA * randomlist
when randomlist
is normal list.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# --- functions ---
def function(row):
randomlist = np.random.randint(1, 30, size=5)
return pd.DataFrame({
'wp': randomlist * row['ConstantA'],
'tempp': randomlist * row['ConstantB'],
'ycp': randomlist * row['tst'],
'yap': randomlist * row['temp'],
'pp': randomlist * row['dtube']
})
# --- main ---
data = {
'ConstantA': [0.01545, 0.6, 0.6, 0.6, 0.6],
'ConstantB': [0.6885e-6, 0, 0, 0, 0],
'tst': [523, 529, 531, 527, 533],
'temp': [523, 524, 524, 524, 524],
'dtube': [0.041, 0.041, 0.041, 0.041, 0.041]
}
df = pd.DataFrame(data)
# before `for`-loop
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Cooled;Tst=523')
ax1.set_ylabel('T(K)')
ax2.set_ylabel('y(C%)')
ax3.set_ylabel('yA ()')
ax3.set_xlabel('w (1000 ka)')
ax4.set_ylabel('E (bar)')
ax4.set_xlabel('w (1000 kg)')
# `for`-loop
for index, row in df.iterrows():
plot3 = function(row)
ax1.plot(plot3['wp'], plot3['tempp'])
ax2.plot(plot3['wp'], plot3['ycp'])
ax3.plot(plot3['wp'], plot3['yap'])
ax4.plot(plot3['wp'], plot3['pp'])
# after `for`-loop
plt.tight_layout() # Automatic adjustment of pyplot so ylabels dont overlap
plt.subplots_adjust(top=0.9) # Adjust plots to put space beween title and subplot
plt.show()
Result: