Python 使用colormap根据线的长度为线添加颜色

Python 使用colormap根据线的长度为线添加颜色,python,matplotlib,Python,Matplotlib,我正在尝试使用颜色映射为行上色,以便渐变颜色指示行的长度。我的当前程序根据行在x轴上的位置为其着色 所有以\u 2结尾的变量都属于对角线行 import numpy as np import matplotlib.pyplot as plt from matplotlib.collections import LineCollection from matplotlib.colors import LinearSegmentedColormap x_nom = [0, 1] y_nom = [

我正在尝试使用
颜色映射为
上色,以便渐变颜色指示
行的长度
。我的当前程序根据行在x轴上的位置为其着色

所有以
\u 2
结尾的变量都属于对角线

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import LinearSegmentedColormap

x_nom = [0, 1]
y_nom = [0, 1]

# x,y coordinates
x1 = 0
x2 = 0.5
y1 = 0
y2 = 1

x1_2 = 0
x2_2 = 1
y1_2 = 0
y2_2 = 1

# create segments from coordinates
x_coords = np.linspace(x1, x2, 100)
y_coords = np.linspace(y1, y2, 100)

x_coords_2 = np.linspace(x1_2, x2_2, 100)
y_coords_2 = np.linspace(y1_2, y2_2, 100)

# "resolution"
res_ar = np.linspace(x1, x2, len(x_coords))
res_ar_2 = np.linspace(x1_2, x2_2, len(x_coords_2))

# reshape array to fit matplotlib segmentation
points = np.array([x_coords, y_coords]).T.reshape(-1, 1, 2)
points_2 = np.array([x_coords_2, y_coords_2]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
segments_2 = np.concatenate([points_2[:-1], points_2[1:]], axis=1)

fig, ax = plt.subplots(figsize=(5,5))

# create custom colormap
cvals = [0., 1.]
colors = ['black', 'white']
norm = plt.Normalize(min(cvals), max(cvals))
tuples = list(zip(map(norm, cvals), colors))
cmap = LinearSegmentedColormap.from_list('', tuples)

# plot segmented line
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc_2 = LineCollection(segments_2, cmap=cmap, norm=norm)
lc.set_array((res_ar))
lc_2.set_array((res_ar_2))
ax.add_collection(lc)
ax.add_collection(lc_2)
plt.show()

如您所见,对角线
线
绘制在完整的
颜色贴图
(颜色区域
0到1
)中。它的
长度
1.41
(x=1
y=1
的平方根)。上方的
行的长度为
1.12
x=0.5,y=1
)。上面的
大约是对角线
的0.8倍。如何使用
colormap
在区域
0到0.8
中为
行上色,而不是(现在)将
0到0.5

编辑:

多亏了tmdavison,我才算出来。此代码根据行的长度为行着色:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import LinearSegmentedColormap

x_nom = [0, 1]
y_nom = [0, 1]

# x,y coordinates
x1 = 0
x2 = 0.8
y1 = 0
y2 = 1

x1_2 = 0
x2_2 = 1
y1_2 = 0
y2_2 = 1

# create segments from coordinates
x_coords = np.linspace(x1, x2, 100)
y_coords = np.linspace(y1, y2, 100)

x_coords_2 = np.linspace(x1_2, x2_2, 100)
y_coords_2 = np.linspace(y1_2, y2_2, 100)

l_nom = np.sqrt(x_nom[1]**2 + y_nom[1]**2)
l = np.sqrt(x2**2 + y2**2)
correct = l/l_nom

# "resolution"
res_ar = np.linspace(x1, x2, len(x_coords)) * correct/x2
res_ar_2 = np.linspace(x1_2, x2_2, len(x_coords_2))

# reshape array to fit matplotlib segmentation
points = np.array([x_coords, y_coords]).T.reshape(-1, 1, 2)
points_2 = np.array([x_coords_2, y_coords_2]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
segments_2 = np.concatenate([points_2[:-1], points_2[1:]], axis=1)

fig, ax = plt.subplots(figsize=(5,5))

# create custom colormap
cvals = [0., 1.]
colors = ['black', 'white']
norm = plt.Normalize(min(cvals), max(cvals))
tuples = list(zip(map(norm, cvals), colors))
cmap = LinearSegmentedColormap.from_list('', tuples)

# plot segmented line
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc_2 = LineCollection(segments_2, cmap=cmap, norm=norm)
lc.set_array((res_ar))
lc_2.set_array((res_ar_2))
ax.add_collection(lc)
ax.add_collection(lc_2)
plt.show()

您可以将用于设置颜色的数据数组标准化为最大值。您可以通过两种方式完成此操作:

  • 当您构造
    res\u ar
    res\u ar\u 2
    数组时

     res_ar = np.linspace(x1, x2, len(x_coords)) / x2
     res_ar_2 = np.linspace(x1_2, x2_2, len(x_coords_2)) / x2_2
    
  • 或者,当您以后使用这些数组时

     lc.set_array((res_ar / x2))
     lc_2.set_array((res_ar_2 / x2_2))
    
  • 执行其中一项操作将意味着色阶始终从0变为1


    严格地说,你不需要为你的
    res\u ar
    数组这样做,因为它已经从0变为1,但我在这里添加它是为了更通用,以防将来行发生变化。

    这是否回答了你的问题?虽然你的回答不能完全解决我的问题,但多亏了你,我才把它弄明白。不仅需要将
    x2
    添加到
    res_ar
    中,还需要将两行的
    长度比
    合并到
    中。请参见编辑部分。无论如何,非常感谢:-)