Python中的 matplotlib(一)繪制簡單的折線圖與散點圖


1.繪制折線圖

 1 import matplotlib.pyplot as plt
 2 
 3 
 4 input_values = [1, 2, 3, 4, 5]
 5 squares = [1, 4, 9, 16, 25]
 6 plt.plot(input_values, squares, linewidth=5)#線寬
 7 
 8 plt.title("Squares Numbers", fontsize=24)#標題及字號
 9 plt.xlabel("Value", fontsize=24)#X軸標題及字號
10 plt.ylabel("Square of Value", fontsize=24)#Y軸標題及字號
11 plt.tick_params(axis='both', labelsize=14)#刻度
12 plt.show()

Figure:

 

2.繪制散點圖

 1 import matplotlib.pyplot as plt
 2 
 3 # x_values = [1, 2, 3, 4, 5]
 4 # y_values = [1, 4, 9, 16, 25]
 5 # plt.scatter(x_values, y_values, s=100)
 6 # #設置圖表標題並給坐標軸加上標簽
 7 # plt.title("Squares of Number", fontsize=24)
 8 # plt.xlabel("Value", fontsize=14)
 9 # plt.ylabel("Square of Value", fontsize=14)
10 # #設置刻度標記的大小
11 # plt.tick_params(axis='both', which='major', labelsize=14)
12 #
13 # plt.show()
14 
15 x_values = list(range(1, 1001))
16 y_values = [x**2 for x in x_values]
17 #去黑色輪廓
18 #plt.scatter(x_values, y_values, c='red',edgecolors='none', s=40)
19 plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues,
20             edgecolors='none', s=40)
21 #設置圖表標題並給坐標軸加上標簽
22 plt.title("Squares of Number", fontsize=24)
23 plt.xlabel("Value", fontsize=14)
24 plt.ylabel("Square of Value", fontsize=14)
25 #設置刻度標記的大小
26 plt.tick_params(axis='both', which='major', labelsize=14)
27 #設置坐標軸的取值范圍
28 plt.axis([0, 1100, 1, 1100000])
29 
30 plt.show()
31 #plt.save('squares_plot.png'(文件名), bbox_inches='tight'(將圖表多余的空白部分剪掉))
32 #用它替換plt.show實現自動保存圖表

Figure:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM