小白学习Python——matplotlib

一、简介

1、Python最常用的绘图库,提供了一整套十分适合交互式绘图的命令API,比较方便的就可以将其嵌入到GUI应用程序中。
2、官网:http://matplotlib.org/
3、学习方式:从官网examples入门学习
http://matplotlib.org/examples/index.html
http://matplotlib.org/gallery.html

二、基本使用

1、使用import导入模块matploylib.pyplot,并简写成plt;使用import导入模块numpy,并简写成np
2、然后,调用plot的.polt方法来绘制一些坐标。这个.plot需要许多参数,但是前两个是‘x’和’y’坐标,我们放入列表。这就意味着,根据这些列表我们拥有三个坐标1,5;2,7和3,4。
3、plt.plot在后台绘制这个绘图,但是绘制了我们想要的一切之后,当我们准备好的时候,我们需要把他带到屏幕上。

	plt.plot([1,2,3],[5,7,4])   #线性图
	plt.show()  #显示图像

输出结果
在这里插入图片描述

三、plt.plot()函数

1、plt.plot(x,y)
2、plt.plot(y) #只传递一个参数的时候,默认为y值,x值默认从0到n
3、plt.plot(x1,y1,x2,y2,x3,y3…) #可以一次项把要画的图像画放在一个plot()函数中
4、plt.plot(x, y, color = ‘red’)#设置图像的颜色为红色,另一种写法:color = ‘#FFFFFF’(通过RGB的方式来调色)
5、plt.plot(x, y, color = ‘red’, linestyle = ‘–’)#linestyle表示图像的线条风格,-表示实线,–表示虚线。。。其他请看help(plt.plot)
6、plt.plot(x, y, ‘r–’)#简写模式,r代表red,–代表虚线

四、Figure

1、Figure:面板(图),matplotlib中的所有图像都是位于figure对象中,一个图像只能有一个figure对象。

	x = np.arange(-3,3,0.1) #x轴的范围是(-3,3),取点连线的间隔是0.1,看上去比较平滑
	y1 = np.sin(x)
	y2 = np.cos(x)
	
	plt.figure()    	#相当于创建一个画布,第一个参数表示画布的编号,第二个是设置画布的尺寸。。。
	plt.plot(x,y1)
	plt.plot(x,y2)		#一个画布上允许有多个图像
	plt.show()

输出结果
在这里插入图片描述

五、刻度、标题、标签和图例

1、灵活使用刻度、标题、标签和图例

	import matplotlib.pyplot as plt
	import matplotlib as mpl
	import numpy as np
	
	x1 = [1,2,3,4,5,6]
	y1 = [5,7,4,6,8,9]
	x2 = [1,2,3,4,5,6]
	y2 = [10,11,12,13,14,15]
	
	plt.figure()
	plt.plot(x1,y1,'ro-',label='出口')
	plt.plot(x2,y2,'bo--',label='进口')
	
	#解决中文显示问题
	mpl.rcParams['font.sans-serif'] = ['SimHei']
	mpl.rcParams['axes.unicode_minus'] = False
	
	#生成图例
	plt.legend()
	
	#设置标题标签
	plt.title('进出口数据')
	plt.xlabel('月份')
	plt.ylabel('美元/亿')
	
	
	#设置x,y轴范围
	plt.xlim(0,7)
	plt.ylim(0,20)
	
	#设置刻度
	plt.xticks([1,2,3,4,5,6],[str(i)+'月' for i in range(1,7)])#前后数据的数量保持一致,前面的6个数表示刻度的个数,后面是刻度下面显示的具体数值
	plt.yticks(np.arange(0,20,2))
	
	#设置坐标轴信息
	ax = plt.gca()
	#设置边框
	ax.spines['right'].set_color('none')#将左边框去掉
	ax.spines['top'].set_color('none')#将上边框去掉
	
	plt.show()

输出结果
在这里插入图片描述

六、Subplot:子图,figure对象下创建一个或多个subplot对象(即axes)用于绘制图像。

1、

    import matplotlib.pyplot as plt
	import matplotlib as mpl
	import numpy as np
	
	x1 = [1,2,3,4,5,6]
	y1 = [5,7,4,6,8,9]
	x2 = [1,2,3,4,5,6]
	y2 = [10,11,12,13,14,15]
	
	#创建图像
	plt.figure()
	plt.subplot(221)#创建子图,’221‘表示是在2行2列的第一个位置
	plt.plot(x1,y1,'ro--')
	plt.subplot(224)#创建子图,’224‘表示是在2行2列的第四个位置
	plt.plot(x2,y2,'bo-')
	
	plt.show()

输出结果
在这里插入图片描述
2、

	#面向对象的形式
	import matplotlib.pyplot as plt
	import matplotlib as mpl
	import numpy as np
	
	x1 = [1,2,3,4,5,6]
	y1 = [5,7,4,6,8,9]
	x2 = [1,2,3,4,5,6]
	y2 = [10,11,12,13,14,15]
	
	#创建图像
	fig = plt.figure()
	
	#创建子图
	ax1 = fig.add_subplot(221)
	ax2 = fig.add_subplot(222)
	ax3 = fig.add_subplot(212)
	
	#在ax上画图
	ax1.plot(np.random.randn(50).cumsum(),'g--')
	ax2.plot(np.random.randn(50).cumsum(),'b--')
	ax3.plot(np.random.randn(50).cumsum(),'r--')
	
	plt.show()

输出结果
在这里插入图片描述
3、subpolts
1、

	import matplotlib.pyplot as plt
	import matplotlib as mpl
	import numpy as np
	fig,axes = plt.subplots(nrows=4,ncols=1,sharex=True,sharey=True)
	
	#axes是一个列表
	axes[0].plot(range(10),'ro-')
	axes[1].plot(range(10),'bo-')
	axes[2].plot(range(10),'go-')
	axes[3].plot(range(10),'ko-')
	
	plt.show()

输出结果
在这里插入图片描述
2、

	import matplotlib.pyplot as plt
	import matplotlib as mpl
	import numpy as np
	
	fig,axes = plt.subplots(2,2)
	
	for i in range(2):
	    for j in range(2):
	        axes[i][j].hist(np.random.randn(100), 10 , color='m', alpha=0.75)#hist表示画直方图
	
	plt.show()

输出结果
在这里插入图片描述
2、

	import matplotlib.pyplot as plt
			import matplotlib as mpl
			import numpy as np
			
	fig,axes = plt.subplots(2,2,sharey=True,sharex=True)#sharey=True,sharex=True表示四个子图共享一个x,y坐标,默认不共享
	
	for i in range(2):
	    for j in range(2):
	        axes[i][j].hist(np.random.randn(100), 10 , color='m', alpha=0.75)#alpha调整透明度
	plt.subplots_adjust(wspace=0.3,hspace=0.3)#设置子图之间的距离
	plt.suptitle('test', fontsize=20)#设置标题,和标题的大小
	
	plt.savefig('1.png',dpi=100)#保存图片——名字+像素
	plt.show()

输出结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值