在Matplotlib中,图形的颜色和背景色都是默认给出,这里介绍下如何设置线条的颜色,为了增加对比,这里我们绘制2条线。
from matplotlib import pyplot as plt
# 主要x 和y的个数要相同,不然会报错
x = [5,6,7,8]
y = [7,3,8,3]
x1 = [2,5,3,9]
y1 = [5,3,2,7]
# 可以设置颜色,g代表green, r代表red,y代表yellow,b代表blue
plt.plot(x, y,'b')
plt.plot(x1, y1,'r')
# 给这个图,添加标题和XY轴名称,注意这地方不能输入中文,matplotlib应该
# 对中文支持不好,写中文,会显示乱码,方块字
plt.title('Epic Chart')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
通过设置颜色,可以让线条显示符合你自己美观的颜色。记住上面不同颜色单词的缩写,你可以试试不同颜色的组合,分别运行下,看看效果。