matplotlib 逻辑回归绘制具有相同颜色的连续曲线

raogr8fs  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(107)
x = np.array([0,1,2,3,4,5])
y = np.array([[0,10],[5,10],[8,10],[10,10],[10,11],[10,11]])

color = [[1,0,0],
         [0,1,0],
         [1,0,0],
         [0,1,0],
         [1,0,0],
         [0,1,0],
         [1,0,0],
         [0,1,0],
         [0,1,0],
         [1,0,0],
         [0,1,0],
         [1,0,0]]
plt.figure(figsize=(2,4))
plt.plot(x, y, lw=0.4, marker = '.', markersize=0)
plt.scatter(np.repeat(x, y.shape[1]), y, s=8, c=np.abs(color))

如何使用逻辑回归连接绿色点和绿色点以及红色点和红色点并获得连续曲线?

xpszyzbs

xpszyzbs1#

我在你的代码中做了一些修改,它起作用了。

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([[0, 10], [5, 10], [8, 10], [10, 10], [10, 11], [10, 11]])

color = [[1, 0, 0],
         [0, 1, 0],
         [1, 0, 0],
         [0, 1, 0],
         [1, 0, 0],
         [0, 1, 0]]
red_indices = np.where(np.array(color) == [1, 0, 0])
green_indices = np.where(np.array(color) == [0, 1, 0])

red_x = x[red_indices[0]]
red_y = y[red_indices[0]][:, 1]
green_x = x[green_indices[0]]
green_y = y[green_indices[0]][:, 1]
plt.figure(figsize=(8, 4))
plt.plot(red_x, red_y, color='red', lw=2, label='Red Curve')
plt.plot(green_x, green_y, color='green', lw=2, label='Green Curve')
plt.scatter(x, y[:, 1], s=50, c=color)

plt.legend()
plt.grid(True)
plt.show()

相关问题