ValueError:to_rgba:使用plot_date()时无效的rgba arg

时间:2017-02-04 14:15:52

标签: python pandas matplotlib

我有一个测试结果的DataFrame,如下所示:

   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

现在我想根据日期对测试进行绘制,以便根据结果使用不同的颜色。这就是我的工作。请注意,我使用this question中概述的方法。我使用的是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的评论,这段代码可行。但有没有办法避免重复并使解决方案更加Pythonic?

# 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()

0 个答案:

没有答案