Python ValueError:to_rgba:使用绘图日期()时rgba参数无效
我有一个测试结果的数据框架,如下所示:Python ValueError:to_rgba:使用绘图日期()时rgba参数无效,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有一个测试结果的数据框架,如下所示: TestName Date IsPassed 0 test1 2017-01-31 21:44:30 0 1 test1 2017-01-31 21:39:00 0 2 test1 2017-01-31 21:38:29 1 3 test1 2017-01-31 21:38:27 1 4 test2 2016-10-
TestName Date IsPassed
0 test1 2017-01-31 21:44:30 0
1 test1 2017-01-31 21:39:00 0
2 test1 2017-01-31 21:38:29 1
3 test1 2017-01-31 21:38:27 1
4 test2 2016-10-31 05:05:02 0
5 test3 2016-12-07 20:58:36 0
6 test3 2016-12-07 20:57:19 0
7 test3 2016-12-07 20:56:15 0
8 test4 2016-12-05 18:50:49 0
9 test4 2016-12-05 18:49:50 0
10 test4 2016-12-05 03:23:09 1
11 test4 2016-12-04 23:51:29 1
现在,我想根据日期绘制测试图,以便根据结果使点具有不同的颜色。这就是我要做的。请注意,我使用中概述的方法。我正在使用Python 3.5
# factorize cases
labels, test = pd.factorize(test_df['TestName'])
tests = test_cases.copy()
tests['TestName'] = labels
# plot data
fig, ax = plt.subplots()
colors = np.where(tests.IsPassed > 0, 'r', 'g')
ax.plot_date(tests.Date, tests.TestName, color=colors)
ax.set_xlim(pd.Timestamp('2016-10-28'), pd.Timestamp('2017-02-04'))
ax.set_ylim(-1, 4)
plt.show()
此代码会导致以下错误:
ValueError: to_rgba:
Invalid rgba arg "['g' 'g' 'r' 'r' 'g' 'g' 'g' 'g' 'g' 'g' 'r' 'r']"
length of rgba sequence should be either 3 or 4
更新
根据@Marcos Modenesi的评论,此代码有效。但是有没有办法避免重复并使解决方案更具python风格呢
# plot data
fig, ax = plt.subplots()
passed = tests[tests.IsPassed == 1]
failed = tests[tests.IsPassed == 0]
ax.plot_date(passed.Date, passed.TestName, color='g')
ax.plot_date(failed.Date, failed.TestName, color='r')
ax.set_xlim(pd.Timestamp('2016-10-28'), pd.Timestamp('2017-02-04'))
ax.set_ylim(-1, 4)
plt.show()
通过阅读,我猜
color
应该是一种单一的颜色,而不是一系列的颜色。我在这里完全是在大声思考,但我会用一种更简单的方式同意你的想法。首先,我用ax.plot\u date(tests.date[passed],tests.TestName[passed],color='g')
wherepassed=np.where(tests.IsPassed>0)
。然后我会用一种等效的方法画出红点。是的,看起来应该是这样。但问题是:如何实现不同结果的不同颜色?我知道你的想法,但我猜应该是这样的:passed=tests[tests.IsPassed==1]failed=tests[tests.IsPassed==0]ax.plot_date(passed.date,passed.TestName,color='g')ax.plot_date(failed.date,failed.TestName,color='r'))
避免重复可以通过for
-循环来完成。for
loops是非音速的吗?通过阅读,我猜color
应该是一种单一的颜色,而不是一系列的颜色。我在这里完全是在大声思考,但我会用一种更简单的方式同意你的想法。首先,我用ax.plot\u date(tests.date[passed],tests.TestName[passed],color='g')
wherepassed=np.where(tests.IsPassed>0)
。然后我会用一种等效的方法画出红点。是的,看起来应该是这样。但问题是:如何实现不同结果的不同颜色?我知道你的想法,但我猜应该是这样的:passed=tests[tests.IsPassed==1]failed=tests[tests.IsPassed==0]ax.plot_date(passed.date,passed.TestName,color='g')ax.plot_date(failed.date,failed.TestName,color='r'))
避免重复可以通过for
-循环来完成。用于循环的是否为非音速?