反向图

时间:2019-03-16 09:30:46

标签: python pandas matplotlib plot

我试图使用Python的matplotlib绘制图形。我想绘制的数据是这个 the data

我试图相对于y_it x轴和t y轴)绘制y_i,但是最终我还是得到了这张图, graph image here

您可以看到它有问题,而且我们都知道(随着我们沿y轴变高,数字越来越小而不是越来越高)。该图应该具有负斜率,如何解决这个问题? (所有帮助将不胜感激)

这是我的代码:

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()

2 个答案:

答案 0 :(得分:1)

如果将NA添加为字符串,则整个数据列将被视为object(字符串的numpy类型)列。因此,y列将按字典顺序排序。

请使用np.nan,在内部将其视为数字:

import numpy as np

data_luruh['y_i'][0] = np.nan
data_luruh['g'][0] = np.nan

答案 1 :(得分:1)

不要为丢失的值使用"NA":它是一个字符串,也会导致其他值也被解释为字符串。而是将行更改为

import numpy as np
data_luruh['y_i'][0] = np.nan
data_luruh['g'][0] = np.nan

给予

graph with negative slope