I want to plot some data. The first column contains the x-data. But matplotlib doesn't plot this. Where is my mistake?
我想画一些数据。第一列包含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()
EDIT: http://paste.pocoo.org/show/205534/ But it doesn't work.
编辑:http://paste.pocoo.org/show/205534/但它不起作用。
3 个解决方案
#1
2
x = data[:][0]
y1 = data[:][2]
y3 = data[:][3]
These lines don't do what you think.
这些台词不像你想的那样。
First they take a slice of the array which is the whole array (that is, just a copy), then they pull out the 0th, 2nd or 3rd ROW from that array, not column.
首先,他们取一个数组的一部分,这是整个数组(也就是一个拷贝),然后他们从数组中取出第0、第二或第三行,而不是列。
You could try
你可以试试
x = [row[0] for row in x]
etc.
等。
#2
5
You can do this by converting data to a numpy array:
您可以通过将数据转换为numpy数组来实现这一点:
data = np.array(data) # insert this new line after your appends
pprint(data)
x = data[:,0] # use the multidimensional slicing notation
y1 = data[:,2]
y3 = data[:,3]
plot(x, y1, x, y3)
A few additional points:
一些额外的点:
You can do the calculation in a more clear and vectorized way using numpy, like this
你可以用一个更清晰的矢量化的方式来计算,就像这样。
data = np.array(data)
rhotang1 = n1*cos(data[:,0]) - n2*cos(data[:,1])
rhotang2 = n1*cos(data[:,0]) + n2*cos(data[:,1])
y3 = rhotang1 / rhotang2
As you wrote it, your calculation may not give what you want since cos
etc take radians as their inputs and your numbers look like degrees.
正如你写的那样,你的计算可能不会给出你想要的东西,因为等着用弧度作为它们的输入,而你的数字看起来像度数。
#3
0
Try this:
试试这个:
#fresnel formula
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 = np.array([
[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],
])
# Populate arrays
x = np.array([row[0] for row in data])
y1 = np.array([row[1] for row in data])
rhotang1 = n1*cos(data[:,0]) - n2*cos(data[:,1])
rhotang2 = n1*cos(data[:,0]) + n2*cos(data[:,1])
y3 = rhotang1 / rhotang2
plot(x, y1, 'r--', x, y3, 'g--')
show()
#1
2
x = data[:][0]
y1 = data[:][2]
y3 = data[:][3]
These lines don't do what you think.
这些台词不像你想的那样。
First they take a slice of the array which is the whole array (that is, just a copy), then they pull out the 0th, 2nd or 3rd ROW from that array, not column.
首先,他们取一个数组的一部分,这是整个数组(也就是一个拷贝),然后他们从数组中取出第0、第二或第三行,而不是列。
You could try
你可以试试
x = [row[0] for row in x]
etc.
等。
#2
5
You can do this by converting data to a numpy array:
您可以通过将数据转换为numpy数组来实现这一点:
data = np.array(data) # insert this new line after your appends
pprint(data)
x = data[:,0] # use the multidimensional slicing notation
y1 = data[:,2]
y3 = data[:,3]
plot(x, y1, x, y3)
A few additional points:
一些额外的点:
You can do the calculation in a more clear and vectorized way using numpy, like this
你可以用一个更清晰的矢量化的方式来计算,就像这样。
data = np.array(data)
rhotang1 = n1*cos(data[:,0]) - n2*cos(data[:,1])
rhotang2 = n1*cos(data[:,0]) + n2*cos(data[:,1])
y3 = rhotang1 / rhotang2
As you wrote it, your calculation may not give what you want since cos
etc take radians as their inputs and your numbers look like degrees.
正如你写的那样,你的计算可能不会给出你想要的东西,因为等着用弧度作为它们的输入,而你的数字看起来像度数。
#3
0
Try this:
试试这个:
#fresnel formula
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 = np.array([
[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],
])
# Populate arrays
x = np.array([row[0] for row in data])
y1 = np.array([row[1] for row in data])
rhotang1 = n1*cos(data[:,0]) - n2*cos(data[:,1])
rhotang2 = n1*cos(data[:,0]) + n2*cos(data[:,1])
y3 = rhotang1 / rhotang2
plot(x, y1, 'r--', x, y3, 'g--')
show()