需要在matplotlib中绘制两种不同颜色的数据

2024-04-29 04:00:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些数据,比如:

x = [0,1,0,0,0,1,0,.....]
w = [5,3,3.4,5,3,5,.....]
y = [1,1,0,1,0,0,0,.....]

我需要绘制(x,w),这个点的颜色应该由y决定,就像我有x = 1y = 1w = 3,那么这个点应该是color1。如果我有x = 1y = 0,那么它应该是color2color3,对于x = 0y = 1color4,对于x = 0y = 0

我试过一些if-else语句,但没有成功

我的代码是:

def plot(x,y,w):
    for (a,b) in zip(x,y):
        if (a,b)==(0,0):
            plt.plot(x,w,'ro')

我想为(x,y)的不同值获得不同的色点


Tags: 数据代码inforifplot颜色def
1条回答
网友
1楼 · 发布于 2024-04-29 04:00:40

可以使用散点图:

import numpy as np
colors = 2 * np.array(x) + np.array(y)
plt.scatter(x,w, c = colors)

这会自动为您选择颜色,如果您要使用自定义颜色,可以执行以下操作:

import numpy as np
import matplotlib.pyplot as plt
colors = 2 * np.array(x) + np.array(y)
plt.scatter(x,w, c = colors)

如果你想连接这些点,你可以:

import numpy as np
import matplotlib.pyplot as plt
colorNames = np.array(['red', 'blue', ...])
colors = 2 * np.array(x) + np.array(y)
plt.scatter(x,w, c = colorNames[colors])
plt.plot(x,w)

这里我认为x和y的0和1的序列是2位的颜色编码

相关问题 更多 >