Matplotlib是一个Python二维绘图库,可以跨平台以各种硬拷贝格式和交互式环境生成出版质量的数据。Matplotlib可用于Python脚本、Python和IPythonshell、Jupyter笔记本、Web应用程序服务器和四个图形用户界面工具包。installpython-mpipinstall-Upippython-mpipinstall-Umatplotlib实例绘制一个简单的图像importnumpyfrommatplotlibimportpyplotx=numpy.arange(1,6)y=2*x+10pyplot.title("Matplotlib")pyplot.xlabel("x轴")pyplot.ylabel("y轴")pyplot.plot(x,y)pyplot.show()从matplotlib导入pyplot#绘制散点图pyplot.scatter(2,6)#设置输出样式pyplot.scatter(3,5,s=200)pyplot.show()Drawaseriesofpointsfrommatplotlibimportpyplotx=[1,5,10,15,20]y=[10,20,30,40,50]pyplot.scatter(x,y,s=100)pyplot.show()自定义颜色matplotlib允许您为散点图中的各个点分配颜色。默认是蓝色点和黑色轮廓,当散点图不包含很多数据点时效果很好。但是当绘制很多点时,黑色轮廓可能会粘在一起。frommatplotlibimportpyplotx=list(range(1,1001))y=[x**2forxinx]pyplot.scatter(x,y,c='red',edgecolor='none',s=40)#设置每个轴的取值范围pyplot.axis([0,1100,0,1100000])pyplot.show()histogramfrommatplotlibimportpyplotimportnumpypyplot.figure(3)x_index=numpy.arange(5)#barindicesx_data=('A','B','C','D','E')y1_data=(20,35,30,35,27)y2_data=(25,32,34,20,25)bar_width=0.35#定义一个数字来表示每个独立条的宽度rects1=pyplot.bar(x_index,y1_data,width=bar_width,alpha=0.4,color='b',label='legend1')#参数:左偏移量,高度,列宽,透明度,颜色,图例rects2=pyplot.bar(x_index+bar_width,y2_data,width=bar_width,alpha=0.5,color='r',label='legend2')#参数:leftOffset,height,columnwidth,transparency,color,legend#关于leftoffset,不需要关心每列的居中,因为只需要在列的中间设置刻度线即可嗯。pyplot.xticks(x_index+bar_width/2,x_data)#x轴刻度线pyplot.legend()#显示图例pyplot.tight_layout()#自动控制图像的外边缘,此方法无法控制图像之间的间隔pyplot.show()线相关属性标记设置线型linestyle或lsdescription'-'实线':'虚线'-'破折号'无','',''什么都不画'-.'点划线标记markerdescription'o'circle'.'点'D'菱形's'正方形'h'六边形1'*'星号'H'六边形2'd'小菱形'_'水平线'v'三角形一角朝下'8'八角形'<'三角形一cornerfacingleft'p'pentagon'>'right-facingtriangle','pixel'^'upward-pointingtriangle'+'plussign'\'竖条'None','',''none'x'Xcolor缩写字符颜色'b'Blue'g'Green'r'Red'y'Yellow'c'Cyan'k'Black'm'Magenta'w'White参考:https://www.9xkd.com/
