Plot graph in Python
Question:
I’m trying to plot the graph between ‘Z’ and ‘SigM’. I’m getting an error. Could anyone please help in plotting the graph.
K = 1
Rmv = 26
SigS = 111.7
M = 2.050
N = 2
SigD = (-249.4)
def Mittelspannung():
result = []
Z = []
SigM = []
for i in range(1,31):
output = 1 - pow((((i-1)*1/14.5)-1),2)
result.append(output)
#print(output)
for value in range(0,15):
C4 = (Rmv) - (result[value]) * (Rmv)
Z.append(C4)
print(C4)
for value in range(15,30):
B11 = (SigD) - (result[value]) * (SigD)
Z.append(B11)
print(B11)
for x in range(0,30):
SigMean = ((SigS**M * (1-(Z[x] + SigS)**N/(Rmv + SigS)**N))*(1/M))/K
SigM.append(SigMean)
return SigM
print(Mittelspannung())
43 return Z, SigM —> 44 plot.plot(Z, SigM) 45 plot.show() 46 print(Mittelspannung()) NameError: name ‘Z’ is not define
Answers:
You should create a list Z
in which you are going to append the Z values computed at each iteration, like this:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
K = 1
Rmv = 26
SigS = 111.7
M = 2.050
N = 2
SigD = (-249.4)
def Mittelspannung():
result = []
Y = []
SigM = []
Z = []
for i in range(1,31):
output = 1 - pow((((i-1)*1/14.5)-1),2)
Z.append(Rmv - (output*Rmv))
result.append(output)
for value in range(0,15):
C4 = (Rmv) - (result[value]) * (Rmv)
Y.append(C4)
for value in range(15,30):
B11 = (SigD) - (result[value]) * (SigD)
Y.append(B11)
for x in range(0,30):
SigMean = ((SigS**M * (1-(Z[x] + SigS)**N/(Rmv + SigS)**N))**(1/M)) / K
SigM.append(SigMean)
return Z, SigM
Z, SigM = Mittelspannung()
plt.figure()
plt.plot(Z, SigM)
You didn’t define Z in the function call. Depending on what tool your using, you should be able to see that in the error message, something like "Z is being used before initializing".
Z, SigM = Mittelspannung()
plt.figure()
plt.plot(Z, SigM)
this code will show you the error
#error
----> 1 Z, SigM = Mittelspannung()
2 plt.figure()
3 plt.plot(Z, SigM)
ValueError: too many values to unpack (expected 2)
// Try this one
SigM = Mittelspannung()
plt.figure()
plt.plot(SigM)
I’m trying to plot the graph between ‘Z’ and ‘SigM’. I’m getting an error. Could anyone please help in plotting the graph.
K = 1
Rmv = 26
SigS = 111.7
M = 2.050
N = 2
SigD = (-249.4)
def Mittelspannung():
result = []
Z = []
SigM = []
for i in range(1,31):
output = 1 - pow((((i-1)*1/14.5)-1),2)
result.append(output)
#print(output)
for value in range(0,15):
C4 = (Rmv) - (result[value]) * (Rmv)
Z.append(C4)
print(C4)
for value in range(15,30):
B11 = (SigD) - (result[value]) * (SigD)
Z.append(B11)
print(B11)
for x in range(0,30):
SigMean = ((SigS**M * (1-(Z[x] + SigS)**N/(Rmv + SigS)**N))*(1/M))/K
SigM.append(SigMean)
return SigM
print(Mittelspannung())
43 return Z, SigM —> 44 plot.plot(Z, SigM) 45 plot.show() 46 print(Mittelspannung()) NameError: name ‘Z’ is not define
You should create a list Z
in which you are going to append the Z values computed at each iteration, like this:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
K = 1
Rmv = 26
SigS = 111.7
M = 2.050
N = 2
SigD = (-249.4)
def Mittelspannung():
result = []
Y = []
SigM = []
Z = []
for i in range(1,31):
output = 1 - pow((((i-1)*1/14.5)-1),2)
Z.append(Rmv - (output*Rmv))
result.append(output)
for value in range(0,15):
C4 = (Rmv) - (result[value]) * (Rmv)
Y.append(C4)
for value in range(15,30):
B11 = (SigD) - (result[value]) * (SigD)
Y.append(B11)
for x in range(0,30):
SigMean = ((SigS**M * (1-(Z[x] + SigS)**N/(Rmv + SigS)**N))**(1/M)) / K
SigM.append(SigMean)
return Z, SigM
Z, SigM = Mittelspannung()
plt.figure()
plt.plot(Z, SigM)
You didn’t define Z in the function call. Depending on what tool your using, you should be able to see that in the error message, something like "Z is being used before initializing".
Z, SigM = Mittelspannung()
plt.figure()
plt.plot(Z, SigM)
this code will show you the error
#error
----> 1 Z, SigM = Mittelspannung()
2 plt.figure()
3 plt.plot(Z, SigM)
ValueError: too many values to unpack (expected 2)
// Try this one
SigM = Mittelspannung()
plt.figure()
plt.plot(SigM)