Reputation: 11
I was trying to plot a graph using Python's matplotlib. The data that I wanted to plot is this
I'm trying to plot y_i
with respect to t
(t
the x axis and y_i
the y axis), but somehow I ended up with this graph,
You can see that there is something wrong with it, and we all know it (as we go higher along the y axis, the number is getting lower instead of higher). The graph is supposed to have negative slope, how do I fix this? (All kind of help will be appreciated)
Here's my code:
import matplotlib.pyplot as plt
import pandas as pd
e = 2.71828183
k = -0.043594
C = 100
h = 5
def y(t):
y = C*(e**(k*t))
return y
def f(Y):
f = k*Y
return f
def y_i(Y, F):
y_i = Y + h*F
return y_i
#Y = y(x)
#F = f(Y)
data_luruh = {}
data_luruh['t'] = [x for x in range(0, 101, 5)]
data_luruh['y'] = [y(x) for x in range(0, 101, 5)]
data_luruh['y_i'] = [0 for x in range(0, 101, 5)]
data_luruh['g'] = [0 for x in range(0, 101, 5)]
data_luruh['y_i'][0] = 'NA'
data_luruh['g'][0] = 'NA'
a = 1
while a <= 20:
data_luruh['y_i'][a] = y_i(y((a-1)*5), f(y((a-1)*5)))
data_luruh['g'][a] = data_luruh['y'][a] - data_luruh['y_i'][a]
a += 1
df = pd.DataFrame(data=data_luruh)
print(df)
t_data = data_luruh['t']
y_data = data_luruh['y']
yi_data = data_luruh['y_i']
plt.xlabel('t')
plt.plot(data_luruh['y_i'], 'go')
plt.show()
Upvotes: 0
Views: 227
Reputation: 3353
Don't use "NA"
for missing values: it's a string and will lead to the other values being interpreted as a string as well. Instead, changing the lines to
import numpy as np
data_luruh['y_i'][0] = np.nan
data_luruh['g'][0] = np.nan
gives
Upvotes: 1
Reputation: 658
If you add NA as a string, the whole data column will be treated as an object
(the numpy type for strings) column. Thus, the y-column will be sorted lexicographically.
Instead, use np.nan
, which internally is treated like a number:
import numpy as np
data_luruh['y_i'][0] = np.nan
data_luruh['g'][0] = np.nan
Upvotes: 1