LINUX.ORG.RU

Помогите осознать код

 , ,


0

2

Есть вот такая штука:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm

x = np.linspace(0, 3 * np.pi, 500)
y = np.sin(x)
z = np.cos(0.5 * (x[:-1] + x[1:])) 

cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)

points = np.array([x, y]).T.reshape(-1, 1, 2) # Почему именно (-1, 1, 2), а не (-1, 2), как она вообще работает? Что за -1? 
segments = np.concatenate([points[:-1], points[1:]], axis=1) #Почему исключаем первую и последнюю точки?

lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(z) # к чему это, мы же синус рисуем
lc.set_linewidth(3)
plt.gca().add_collection(lc)

plt.xlim(x.min(), x.max())
plt.ylim(-1.1, 1.1)
plt.show()

★★★★★

Последнее исправление: deterok (всего исправлений: 1)

1. http://www.scipy.org/Tentative_NumPy_Tutorial

Shape Manipulation.
...
If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated

2. внимательно посмотреть на то, что выведет этот код:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm

x = np.linspace(0, 3 * np.pi, 10)
y = np.sin(x)
z = np.cos(0.5 * (x[:-1] + x[1:])).

cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)

points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

print points
print segments
3. http://nullege.com/codes/search/matplotlib.collections.LineCollection
# Create the line collection object, setting the colormapping parameters.
# Have to set the actual values used for colormapping separately.
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(z)
lc.set_linewidth(3)
для наглядности:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm

x = np.linspace(0, 3 * np.pi, 500)
y = np.sin(x)
z = np.cos(0.5 * (x[:-1] + x[1:])).
x2= (x[:-1] + x[1:])/2

cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)

points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(z)
lc.set_linewidth(3)
plt.gca().add_collection(lc)

points = np.array([x2, z]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc2 = LineCollection(segments, cmap=cmap, norm=norm)
lc2.set_array(z)
lc2.set_linewidth(1)
plt.gca().add_collection(lc2)

plt.xlim(x.min(), x.max())
plt.ylim(-1.1, 1.1)
plt.show()
особое внимание уделить строчкам:

cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.