Matplotlib,绘制x1和y,但是在python中基于x2的颜色代码?

时间:2018-12-22 23:44:15

标签: python matplotlib plot

我正在尝试针对y创建x1的图,并根据另一个变量x2使点着色。

$("#ReportReasonModal").modal();

每个变量具有相同的包含数据并具有相同的长度。关于x2(triage)的特殊之处在于,它从1-5编号,并希望根据这些颜色进行着色,因此1是红色,2是橙色,3是黄色,4是绿色和5是蓝色。

代码已更新

x1 = times
y = wait
x2 = triage (from 1-5)

triage = colors [key])

X = dataset.iloc[:, 0:3].values
y = dataset.iloc[:, 3].values

triage = X[:, 0]
week = X[:, 1]
times = X[:, 2]
wait = y

df = pd.DataFrame(dict(times=times, wait=wait, triage=triage))
fig, ax = plt.subplots()
colors = {'1':'red', '2':'orange', '3':'yellow', '4':'green', '5':'blue'}

grouped = df.groupby('triage')
for key, group in grouped:
    group.plot(ax=ax, kind='scatter', x='times', y='wait', label=key, 

设法尝试了一些东西,但仍然不能正常工作,任何人都可以看到这里可能出了什么问题?

1 个答案:

答案 0 :(得分:0)

首先,提供complete and verifiable example很重要。这里的数据依赖关系不允许这样做。但是,要回答您的问题,您需要索引您的条件。我为您制作了一些模拟数据,以产生所需的结果。

import pandas as pd, numpy as np, matplotlib.pyplot as plt
# mockdata
data = np.zeros((10, 3))
data[:, :2] = np.random.rand(len(data), 2)
data[:, -1] = np.random.randint(0, 2, size = len(data))
df = pd.DataFrame(data, columns = 'x y condition'.split(' '))
fig, ax = plt.subplots()
# index into different subsets and plot
for condition, selection in df.groupby('condition'):
    ax.plot(selection.x, selection.y, label = condition)
ax.legend(title = 'condition')
ax.set(xlabel = 'x', ylabel = 'y')
fig.show()

enter image description here