Python:使用matplotlib的x-y-plot

我想绘制一些数据.第一列包含x数据.但matplotlib没有绘制这个.我的错误在哪里?

import numpy as np
from numpy import cos
from scipy import *
from pylab import plot, show, ylim, yticks
from matplotlib import *
from pprint import pprint

n1 = 1.0
n2 = 1.5

#alpha, beta, intensity
data = [
    [10,    22,     4.3],
    [20,    42,     4.2],
    [30,    62,     3.6],
    [40,    83,     1.3],
    [45,    102,    2.8],
    [50,    123,    3.0],
    [60,    143,    3.2],
    [70,    163,    3.8],
    ]

for i in range(len(data)):
    rhotang1 = (n1 * cos(data[i][0]) - n2 * cos(data[i][1]))
    rhotang2 = (n1 * cos(data[i][0]) + n2 * cos(data[i][1]))
    rhotang = rhotang1 / rhotang2
    data[i].append(rhotang) #append 4th value

pprint(data)
x = data[:][0]
y1 = data[:][2]
y3 = data[:][3]
plot(x, y1, x, y3)
show()

编辑:http://paste.pocoo.org/show/205534/
但它不起作用.

最佳答案

x = data[:][0]
y1 = data[:][2]
y3 = data[:][3]

这些线条不符合你的想法.

首先,他们取一个整个数组的数组(即只是一个副本),然后从该数组中取出第0,第2或第3行,而不是列.

你可以试试

x = [row[0] for row in x]

等等

点赞