matplotlib模块练习,不同颜色设置柱

#导入模块
import matplotlib.pyplot as plt
import numpy as np

#生成x y
np.random.seed(0)
x=np.arange(5)
y=np.random.randint(-5,5,5)
#添加颜色
v_bar=plt.bar(x,y,color='blue')

#对y值大于0设置为蓝色  小于0的柱设置为绿色
for bar,height in zip(v_bar,y):
    if height<0:
        bar.set(color='green')

plt.show()

matplotlib模块练习,不同颜色设置柱_第1张图片

你可能感兴趣的:(matplotlib模块练习,不同颜色设置柱)