matplotlib折线图整个绘图详细过程(由简到细)

几种图的特点:
折线图:能够显示事物的变化趋势,变化情况 plt.plot()
直方图:绘制连续性的数据,展示一组或者多组数据的分布情况
条形图:绘制离散的数据,展示一组或多组数据的分布情况
散点图:判断变量之间是否存在数量关联趋势,展示离群点,分布规律 plt.scatter()

折线图绘制:

一次完整简单绘图:

	from matplotlib import pyplot as plt
	x = range(1, 21, 1) #得到x轴坐标
	y = [num = radom.randint(10, 20) for i in range(20)]  #得到y轴坐标,x,y坐标个数必须一致
	
	##设置图片大小,也可以不用设置   可省去用默认
	plt.figure(figsize=(20,8), dpi=100) #figsize设置图片大小,dpi设置图片清晰度
	
	##设置x,y轴坐标刻度         可省去,用默认
	plt.xticks(range(1, 21, 2)) #x坐标轴的显示值
	plt.yticks(range(min(y), max(y), 2))  #y坐标轴的显示值
	
	##绘制图
	plt.plot(x, y)  #传进去x,y的坐标
	plt.show() #把图显示出来

	##保存图片
	plt.savefig("图片存储路径")

plt.xticks()方法:

1、设置中文字体,两种方法:

1)plt.xticks(x[::3], x_label[::3], rotation=45, fontproperties=my_font)
x为列表,是显示出来的刻度值
x_label是列表,是与刻度值对应的文字内容,可以将对应位置的列表值改为对应文字内容
rotation是显示出来的刻度值或者对应的文字内容旋转一定的角度,值为多少就旋转多少
fontproperties是将实例化的matplotlib.font_manager.FontProperties()的实例传进来,用于使用实例化方法的字体设置,matplotlib.font_manager.FontProperties()的使用方法是:my_font = font_manager.FontProperties(fname=“C:\Windows\Fonts\STKAITI.TTF”),然后将my_font传进plt.xticks对应的参数进行了,fname的值是系统中对应字体的文件目录。

这个方法是每个地方都要传入实例化的字体设置my_font,就是说在设置x轴的汉字处需要传,y轴的汉字出需要传,title处需要传。

		完整的一次使用过程是:
			from matplotlib import pyplot as plt
			import random
			import matplotlib
			from matplotlib import font_manager
			
			#实例化字体,fname的值是系统中对应字体的文件目录
			my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\STKAITI.TTF")
			
			
			#设置x、y轴坐标值
			random.seed(10)
			y = [random.randint(25, 30) for i in range(121)]
			x = list(range(1,122))
			# print(y, '\n', x)
			# print(len(x), len(y))
			
			设置图片大小
			plt.figure(figsize=(20, 8), dpi=100)
			
			
			#设置x轴坐标对应的文字内容
			x_label = ["1月{}日".format(i) for i in range(1, 32)]
			x_label += ["2月{}日".format(i) for i in range(1, 30)]
			x_label += ["3月{}日".format(i) for i in range(1, 32)]
			x_label += ["4月{}日".format(i) for i in range(1, 31)]
			# plt.xticks(x[::3], x_label[::3], rotation=45, fontproperties=my_font)
			plt.xticks(x[::3], x_label[::3], rotation=45)
			
			#绘图
			plt.plot(x, y)
			plt.show()

2)font = { ‘family’: ‘STKAITI’, ###STKAITI是华文楷体再系统中的名字
‘weight’: ‘bold’,
‘size’: 18} ###size是字体大小
matplotlib.rc(“font”, **font)

这个方法的好处是设置好,后面的所有都会默认只用这种字体。

		完整的一次使用过程:
		from matplotlib import pyplot as plt
		import random
		import matplotlib
		from matplotlib import font_manager
		
		
		#设置中文字
		font = {'family': 'STKAITI',
				'weight': 'bold',     
				'size': 25} 
		matplotlib.rc("font", **font)

		#设置x、y轴坐标值
		random.seed(10)
		y = [random.randint(25, 30) for i in range(121)]
		x = list(range(1,122))
		# print(y, '\n', x)
		# print(len(x), len(y))

		#设置图片大小
		plt.figure(figsize=(20, 8), dpi=100)


		#设置x、y轴坐标值对应的文字内容
		x_label = ["1月{}日".format(i) for i in range(1, 32)]
		x_label += ["2月{}日".format(i) for i in range(1, 30)]
		x_label += ["3月{}日".format(i) for i in range(1, 32)]
		x_label += ["4月{}日".format(i) for i in range(1, 31)]
		plt.xticks(x[::3], x_label[::3], rotation=45)

		#绘图
		plt.plot(x, y)
		plt.show()

2、设置图形描述信息:

		plt.xlabel("描述内容") #对x轴描述
		plt.ylabel("描述内容") #对y轴描述
		plt.title("描述内容")  #整个图形的名字
	
	完整过程:
		from matplotlib import pyplot as plt
		import random
		import matplotlib
		from matplotlib import font_manager
		
		
		#设置中文字
		font = {'family': 'STKAITI',
				'weight': 'bold',
				'size': 25}
		matplotlib.rc("font", **font)

		#设置x、y轴坐标值
		random.seed(10)
		y = [random.randint(25, 30) for i in range(121)]
		x = list(range(1,122))
		# print(y, '\n', x)
		# print(len(x), len(y))

		#设置图片大小
		plt.figure(figsize=(20, 8), dpi=100)


		#设置x、y轴坐标值对应的文字内容
		x_label = ["1月{}日".format(i) for i in range(1, 32)]
		x_label += ["2月{}日".format(i) for i in range(1, 30)]
		x_label += ["3月{}日".format(i) for i in range(1, 32)]
		x_label += ["4月{}日".format(i) for i in range(1, 31)]
		plt.xticks(x[::3], x_label[::3], rotation=45)
		
		#图形描述信息
		plt.xlabel("日期")
		plt.ylabel("温度 单位(℃)")
		plt.title("日平均温度")

		#绘图
		plt.plot(x, y)
		plt.show()

3、设置网格线:

	plt.grid(alpha=0.5) 
	##参数alpha是网格线透明度
	
	完整实例:
	#设置中文字体
	font = {'family': 'STKAITI',
			'weight': 'bold',
			'size': '18'}

	matplotlib.rc("font", **font)



	#设置x、y轴坐标值
	y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
	y_2 = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
	x = list(range(11, 31))
	
	#设置图像大小
	plt.figure(figsize=(20, 10), dpi=100)


	#绘制x、y轴刻度对应的文字
	x_label = ["{}岁".format(i) for i in x]
	plt.xticks(x, x_label, rotation=45)
	y_label = list(range(min(y_1), max(y_1)+1))
	plt.yticks(y_label)

	#设置x,y,图的描述信息
	plt.xlabel("年龄")
	plt.ylabel("交往对象个数")
	plt.title("随年龄变化的对象交往情况图")


	#绘制网格线
	plt.grid(alpha = 0.4)
	
	绘制图像
	plt.plot(x, y_1)
	plt.plot(x, y_2) ##一个图像中可以绘制两条线,多条线类似

	plt.show()

4、设置图例

	plt.legend(loc="upper right") 
	##在plt.plot(label="线条名字")添加label参数,描述线条名字。loc可以设置图例的具体位置,可以不设置,默认寻找最佳位置(线条少的地方
	
	
	#设置中文字体
	font = {'family': 'STKAITI',
			'weight': 'bold',
			'size': '18'}

	matplotlib.rc("font", **font)



	#设置x、y轴坐标值
	y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
	y_2 = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
	x = list(range(11, 31))
	
	#设置图像大小
	plt.figure(figsize=(20, 10), dpi=100)


	#绘制x、y轴刻度对应的文字
	x_label = ["{}岁".format(i) for i in x]
	plt.xticks(x, x_label, rotation=45)
	y_label = list(range(min(y_1), max(y_1)+1))
	plt.yticks(y_label)

	#设置x,y,图的描述信息
	plt.xlabel("年龄")
	plt.ylabel("交往对象个数")
	plt.title("随年龄变化的对象交往情况图")


	#绘制网格线
	plt.grid(alpha = 0.4)
	
	绘制图像
	plt.plot(x, y_1, label="我")
	plt.plot(x, y_2, label="简")
	plt.legend(loc="upper right")
	''' 
	#此处可以这样:
	plt.plot(x, y_1)
	plt.plot(x, y_2)
	plt.legend(labels=["我", "简"], loc="upper right")
	#效果相同
	'''

	plt.show()

5、自定义绘制的图线风格:

plt.plot(x, y,

color = "颜色" #可以使用英文,也可以使用颜色16进制代码
linestyle = "-." #设置线条样式
linewidth = num  #设置线条粗细,num是一个数字
alpha = double_num #设置透明度,double_num是一个0~1的小数

)
	

完整过程:
	#设置中文字体
	font = {'family': 'STKAITI',
			'weight': 'bold',
			'size': '18'}

	matplotlib.rc("font", **font)



	#设置x、y轴坐标值
	y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
	y_2 = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
	x = list(range(11, 31))
	
	#设置图像大小
	plt.figure(figsize=(20, 10), dpi=100)


	#绘制x、y轴刻度对应的文字
	x_label = ["{}岁".format(i) for i in x]
	plt.xticks(x, x_label, rotation=45)
	y_label = list(range(min(y_1), max(y_1)+1))
	plt.yticks(y_label)

	#设置x,y,图的描述信息
	plt.xlabel("年龄")
	plt.ylabel("交往对象个数")
	plt.title("随年龄变化的对象交往情况图")


	#绘制网格线
	plt.grid(alpha = 0.4)
	
	绘制图像
	plt.plot(x, y_1, label="我", color="orange", linestyle=":", linewidth=1)
	plt.plot(x, y_2, label="简", color="black", linestyle="-.", linewidth=20)
	plt.legend(loc="upper right")

	plt.show()

你可能感兴趣的:(python基础,python,可视化)