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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章