python__matplotlib的安装和画简单图

matplotlib安装:

安装一定要先看官方的安装文档,不行再查别的教程,不止一次发现安装文档还是官方的最靠谱matplotlib官方文档
pyplot官方文档

基本语法:

from matplotlib import pyplot as plt
x=[1,2,3,1]
y=[1,3,2,1]#输入x,y坐标
plt.plot(x,y)
plt.xlabel(u'x axis')
plt.ylabel(u'y axis')#给x,y标签加label
plt.xticks([1,4])
#plt.xticks([-1,4],['a','b'])
plt.yticks([1,4])#定义坐标轴刻度
plt.grid(True,color='b')#画网格
plt.xlim([-1,4])
plt.ylim([-1,4])# set the *x* limits of the current axes
plt.show()

结果:

结果

改变线的样式:

from matplotlib import pyplot as plt
import numpy as np
x=[1,2,3,1]
y=[1,3,2,1]
plt.plot(x,y,color="r",linestyle='--',label="red")
#linestye虚实线
#label 图例标签
#linewidth 线的粗细
#marker 点的形状(marker类型参考 http://www.labri.fr/perso/nrougier/teaching/matplotlib/#line-properties)
plt.plot(np.array(x)+0.5,np.array(y)+0.5,linewidth='3',color="g",marker='o',label="green")
#将x改为nump的数组后,可以支持广播(broadcast)运算,该运算可以运用在数组的所有元素上
plt.xticks(range(5))
plt.yticks(range(5))
plt.legend(loc="upper left")
#legend是显示图例,loc参数是位置,默认是显示在右上角的
plt.grid(True,color="b")
plt.show()

结果

结果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容