Python: xy-plot with matplotlib

I want to build some data. The first column contains x-data. But matplotlib does not do this. Where is my mistake?

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 this does not work.

+3
source share
3 answers
x = data[:][0]
y1 = data[:][2]
y3 = data[:][3]

These lines do not do what you think.

First they take a slice of the array, which is the whole array (i.e. just a copy), then they pull the 0th, 2nd or 3rd ROW from this array, and not into the column.

You can try

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

and etc.

+2
source

You can do this by converting the data to a numpy array:

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:

, numpy,

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

, , , cos .. , .

+5

Try the following:

#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()
0
source

Source: https://habr.com/ru/post/1742471/


All Articles