I want to create a Graph in MatPlotLib, where the Graph variables are an equation (specifically e ^ T*x . Over Here, T is a changing variable based on what the user has input). My code is:
def conshazmodel_Reliability (nofail, totaltime):
x = np.linspace(0,totaltime 1,200)
conshazmodel_Reliability_var1 = nofail/totaltime
conshazmodel_Reliability_var2 = 1/conshazmodel_Reliability_var1
var2 = - conshazmodel_Reliability_var2
conshazmodel_Reliability_equation = f"e ^ {var2}*x"
print(f"Constant Hazard Rate Model Reliability Function is: {conshazmodel_Reliability_equation}")
userinput = input("Would you like to graph (Y or N): ")
if userinput == "Y":
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.plot(x, conshazmodel_Reliability_equation, 'y', label = conshazmodel_Reliability_equation)
plt.legend(loc='upper left')
elif userinput == "N":
print("Finished Constant Hazard Rate Model Reliability Function.")
else:
print("Did not Understand.")
I am using MatPlotLib to graph my data, and am using Numpy for the exponential part. I should get something like: An Image of Exp Graph Required
I am using VsCode for anyone that wants to know. The error that i get is:
Traceback (most recent call last):
File "c:/Users/mghaf/Desktop/Python Codes/ReMan/ReMan/test.py", line 3, in <module>
ReMan.conshazmodel_Reliability(15, 7)
File "c:\Users\mghaf\Desktop\Python Codes\ReMan\ReMan\__init__.py", line 168, in conshazmodel_Reliability
plt.plot(x, conshazmodel_Reliability_equation, 'y', label = conshazmodel_Reliability_equation)
File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\matplotlib\pyplot.py", line 3021, in plot
**({"data": data} if data is not None else {}), **kwargs)
File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\matplotlib\axes\_axes.py", line 1605, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\matplotlib\axes\_base.py", line 315, in __call__
yield from self._plot_args(this, kwargs)
File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\matplotlib\axes\_base.py", line 501, in _plot_args
raise ValueError(f"x and y must have same first dimension, but "
EDIT:
The new code that i wrote without any error is:
def conshazmodel_Reliability (nofail, totaltime):
x = np.linspace(0,totaltime 1,200)
conshazmodel_Reliability_var1 = nofail/totaltime
conshazmodel_Reliability_var2 = 1/conshazmodel_Reliability_var1
var2 = - conshazmodel_Reliability_var2
conshazmodel_Reliability_equation_string = f"e ^ {var2}*x"
conshazmodel_Reliability_equation = np.exp(var2*x)
print(f"Constant Hazard Rate Model Reliability Function is: {conshazmodel_Reliability_equation_string}")
userinput = input("Would you like to graph (Y or N): ")
if userinput == "Y":
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.plot(x, conshazmodel_Reliability_equation, 'y', label = conshazmodel_Reliability_equation_string)
plt.legend(loc='upper left')
elif userinput == "N":
print("Finished Constant Hazard Rate Model Reliability Function.")
else:
print("Did not Understand.")
Now, there is no error. However, there is also no visual output. Maybe I need to change the equation?
CodePudding user response:
The main issue from the first line is that conshazmodel_Reliability_equation
is a string and not an equation. This needs to be changed into equation format. It has to be (as @Trenton McKinney said), needs to be conshazmodel_Reliability_equation = np.exp(var2)*x
The second issue is that there is no plt.show()
for the graph to be shown.