Python編程:從入門到實踐 - matplotlib篇 - plot & scatter

matplotlib篇 plot & scatter

# filename.py 獲取當前文件名方法
import sys

# 當前文件名
print(sys.argv[0])

# 去除後綴後的文件名
print(sys.argv[0].split('.')[0])

 

 

# mpl_squares.py 簡單的平方折線圖
import matplotlib.pyplot as plt
import sys

input_values = [x for x in range(1, 6)]
squares = [x ** 2 for x in range(1, 6)]

# 函數plot嘗試根據數字繪製除有意義的圖形
# linewidth參數設置線條寬度
plt.plot(input_values, squares, linewidth=2)

# 設置圖標標題,並給座標軸加上標籤
plt.title('Square Numbers', fontsize=24)
plt.xlabel('Value', fontsize=14)
plt.ylabel('Square of Value', fontsize=14)

# 設置刻度標記的大小
plt.tick_params(axis='both', labelsize=14)

# 函數show打開matplotlib查看器,並顯示出繪製的圖形
# plt.show()

plt.savefig('images/' + sys.argv[0].split('.')[0] + '.png', bbox_inches='tight')

 

 

# scatter_squares.py 使用scatter繪製散點圖並設置其樣式

import matplotlib.pyplot as plt
import sys

x_values = [x for x in range(1, 101)]
y_values = [x ** 2 for x in range(1, 101)]

# edgecolor參數 -- 點輪廓
# c參數 -- 點顏色(接受RGB值(必須是三個0~1之間的小數值組成的元組)或'red'等) 值越接近0,指定的顏色越深,值越接近1,指定的顏色越淺。
# s參數 -- 點大小
#plt.scatter(x_values, y_values, edgecolor='none', s=10, c=(0.4, 0.4, 0))

# 將c參數設置爲y列表,並使用參數cmap告訴pyplot使用哪一個顏色映射(漸變)
plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, edgecolor='none', s=10)

# 設置圖標標題並給座標加上標籤
plt.title('Squares Numbers', fontsize=24)
plt.xlabel('Value', fontsize=14)
plt.ylabel('Square of Value', fontsize=14)

# 設置刻度標記的大小
plt.tick_params(axis='both', which='major', labelsize=14)

# 自動保存圖表
plt.savefig('images/' + sys.argv[0].split('.')[0] + '.png', bbox_inches='tight')
# 第一個實參指定要以什麼樣的文件名保存圖表
# 第二個實參指定將圖表多餘的空白區域裁剪掉

 

 

# mpl_cubes 立方折線圖

import matplotlib.pyplot as plt
import sys

x_values = [x for x in range(1, 6)]
y_values = [y ** 3 for y in range(1, 6)]

plt.plot(x_values, y_values, color='y', linestyle='--', linewidth=2)

plt.xlabel('Cubes', fontsize=14)
plt.ylabel('Values', fontsize=14)
plt.title('Map for Cubes', fontsize=14)

plt.tick_params(axis='both', labelsize=10)

plt.savefig('images/' + sys.argv[0].split('.')[0] + '.png', bbox_inches='tight')

 

 

# scatter_cubes.py 立方散點圖

import matplotlib.pyplot as plt
import sys

x_values = [x for x in range(1, 101)]
y_values = [y ** 3 for y in range(1, 101)]

plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Reds, s=10, edgecolor='none')

plt.xlabel('Values', fontsize=14)
plt.ylabel('Cubes', fontsize=14)
plt.title('Map for Cubes', fontsize=24)

plt.tick_params(axis='both', labelsize=10)

plt.savefig('images/' + sys.argv[0].split('.')[0] + '.png', bbox_inches='tight')

 

相關文章
相關標籤/搜索