将两个列表绘制为一行不起作用

时间:2015-05-21 17:36:03

标签: python matplotlib

我有能够创建两个数字列表的函数。

使用matplotlip我可以将这两个列表绘制为点,但是当我尝试绘制为一条线(格式化程序:'-')时,生成的图形只是空白。

这里有什么问题?

import matplotlib.pyplot as plt

var = "1.460.362.021.640.560.361.010.520.240.342.680.622.670.850.411.051.880.030.970.502.194.762.872.040.262.140.601.350.470.150.111.010.070.020.561.490.631.281.510.131.611.271.150.521.180.722.080.831.350.761.321.990.620.353.480.541.131.910.130.801.991.012.350.470.162.421.231.921.050.231.871.020.130.750.740.060.310.061.041.301.530.421.662.783.151.931.011.971.790.952.090.110.931.231.370.761.091.595.040.322.330.171.930.310.100.161.510.340.231.202.580.301.481.610.503.840.120.451.160.00"
rain = []
year = [1895]
e = 1895
i = 0

def rainv(i,var,rain):
    while i < 477:
        a = float(var[0 + i :4 + i])
        i = i + 4
        rain.append(a)
        if i  >= 476:
            return rain

def theyear(year, e):
    while e < 2015:
            e = e + 1
            year.append(e)
            if e == 2015:
                return year

rainv(i,var,rain)
theyear(year,e)
print(rain)
print(year)

plt.plot([year],[rain], '-')
plt.axis([1885,2014, 0,6])
plt.show()

1 个答案:

答案 0 :(得分:3)

您不小心将列表列表作为参数传递给了

&#39;一年&#39;并且&#39;下雨&#39;已经是列表的形式,所以只需更改以下行

plt.plot([year],[rain], '-')

plt.plot(year,rain, '-')

更多解释:

使用[year],传递的列表如下: -

[[1897,...., 2015]]

但你需要传递类似

的内容
[1897,...., 2015]

还要确保x和y保持相同的尺寸。在你提供的代码中,它们不是,一个列表的长度是119,而另一个是121

欢呼声