【发布时间】:2021-07-08 09:12:10
【问题描述】:
我有一个随机正负值的条形图。我想在条形图中将所有负值设置为蓝色,将所有正值设置为红色。 如何将负值变为蓝色,将正值变为红色?
这是我迄今为止尝试过的,但我得到一个错误:
rand = np.random.randint(-2, 2, (30))
time = np.arange(1,31,1)
plt.bar(time, rand)
plt.show()
if rand < 0:
plt.bar(time, rand,'blue')
elif rand > 0:
plt.bar(time, rand,'red')
plt.show()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-110-2154dd329d10> in <module>
----> 1 if rand < 0:
2 plt.bar(time, rand,'blue')
3 elif rand > 0:
4 plt.bar(time, rand,'red')
5 plt.show()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
【问题讨论】:
标签: python numpy matplotlib jupyter