文章程式碼顯示

2018年7月2日 星期一

一起學 Python 99 - matplotlib 使用


import numpy as np
import matplotlib.pyplot as plt

from matplotlib.ticker import MultipleLocator, FormatStrFormatter
 
xmajorLocator   = MultipleLocator(10) #設置X主刻度的倍數
xminorLocator   = MultipleLocator(5) #設置X次刻度的倍數
 
ymajorLocator   = MultipleLocator(10) 
yminorLocator   = MultipleLocator(5) 

with open('D:/USER/Desktop/sanguo/data1/1-6.txt') as data6T:# 打開指定路徑文件
    data6 = data6T.readlines()#將打開文件的內容逐行讀到內存中,with在執行完命令後會關閉文件

with open('D:/USER/Desktop/sanguo/data1/1-7.txt') as data7T:
    data7 = data7T.readlines()

print("Get data Done!\n")
print(data6)
print(data7)

S6Num = 0
S6_X_array = []
S6_Y_array = []
T6Num = 0
T6_X_array = []
T6_Y_array = []
I6Num = 0
I6_X_array = []
I6_Y_array = []

S7Num = 0
S7_X_array = []
S7_Y_array = []
T7Num = 0
T7_X_array = []
T7_Y_array = []
I7Num = 0
I7_X_array = []
I7_Y_array = []

# 判定各等級各種類個數
for i in range(0,len(data6)):
   if data6[i][0] == 'S':
      S6Num = S6Num + 1
   elif data6[i][0] == 'T':
      T6Num = T6Num + 1
   elif data6[i][0] == 'I':
      I6Num = I6Num + 1

for i in range(0,len(data7)):
   if data7[i][0] == 'S':
      S7Num = S7Num + 1      
   elif data7[i][0] == 'T':
      T7Num = T7Num + 1   
   elif data7[i][0] == 'I':
      I7Num = I7Num + 1

LV6Num = [S6Num,T6Num,I6Num]
LV7Num = [S7Num,T7Num,I7Num]

# LV6  
for i in range(0,len(data6)):
   t = data6[i].find('t')
   c = data6[i].find(',')
   n = data6[i].find('\n')
   if data6[i][0] == 'S':      
      S6_X_array.append(int(data6[i][t+1:c]))
      S6_Y_array.append(int(data6[i][c+1:n]))
   elif data6[i][0] == 'T':
      T6_X_array.append(int(data6[i][t+1:c]))
      T6_Y_array.append(int(data6[i][c+1:n]))
   elif data6[i][0] == 'I':
      I6_X_array.append(int(data6[i][t+1:c]))
      I6_Y_array.append(int(data6[i][c+1:n]))
# LV7  
for i in range(0,len(data7)):
   t = data7[i].find('t')
   c = data7[i].find(',')
   n = data7[i].find('\n')
   if data7[i][0] == 'S':      
      S7_X_array.append(int(data7[i][t+1:c]))
      S7_Y_array.append(int(data7[i][c+1:n]))
   elif data7[i][0] == 'T':
      T7_X_array.append(int(data7[i][t+1:c]))
      T7_Y_array.append(int(data7[i][c+1:n]))
   elif data7[i][0] == 'I':
      I7_X_array.append(int(data7[i][t+1:c]))
      I7_Y_array.append(int(data7[i][c+1:n]))    

plt.figure(figsize=(8,6),dpi=150)
ax = plt.subplot(111)
xmin,xmax = 840, 999
ymin,ymax = 630, 850
plt.xlim(xmin,xmax)
plt.ylim(ymin,ymax)

# Stone
for i in range(0,len(S6_X_array)):
   plt.plot(S6_X_array[i],S6_Y_array[i],"rs",ms=4)
   plt.text(S6_X_array[i]+0.7, S6_Y_array[i]-0.7, u'6',color='r')
for i in range(0,len(S7_X_array)):
   plt.plot(S7_X_array[i],S7_Y_array[i],'r*',ms=8)
   plt.text(S7_X_array[i]+0.7, S7_Y_array[i]-0.7, u'7',size=14,color='r')
   
# Tree
for i in range(0,len(T6_X_array)):
   plt.plot(T6_X_array[i],T6_Y_array[i],"bs",ms=4)
   plt.text(T6_X_array[i]+0.7, T6_Y_array[i]-0.7, u'6',color='b')
for i in range(0,len(T7_X_array)):
   plt.plot(T7_X_array[i],T7_Y_array[i],'b*',ms=8)
   plt.text(T7_X_array[i]+0.7, T7_Y_array[i]-0.7, u'7',size=14,color='b')
   
# Iron
for i in range(0,len(I6_X_array)):
   plt.plot(I6_X_array[i],I6_Y_array[i],"gs",ms=4)
   plt.text(I6_X_array[i]+0.7, I6_Y_array[i]-0.7, u'6',color='g')
for i in range(0,len(I7_X_array)):
   plt.plot(I7_X_array[i],I7_Y_array[i],'g*',ms=8)
   plt.text(I7_X_array[i]+0.7, I7_Y_array[i]-0.7, u'7',size=14,color='g')

# === PLOT ===
# 畫點
plt.plot(S6_X_array[0],S6_Y_array[0],"rs",label="Stone6",ms=4)
plt.plot(S7_X_array[0],S7_Y_array[0],'r*',label="Stone7",ms=8)
plt.plot(T6_X_array[0],T6_Y_array[0],"bs",label="Tree6",ms=4)
plt.plot(T7_X_array[0],T7_Y_array[0],'b*',label="Tree7",ms=8)
plt.plot(I6_X_array[0],I6_Y_array[0],"gs",label="Iron6",ms=4)
plt.plot(I7_X_array[0],I7_Y_array[0],'g*',label="Iron7",ms=8)

# 畫 圓
theta = np.linspace(0, 2*np.pi,800)
R = 15 # 60秒區
x = R*np.cos(theta)+(xmin+xmax)/2
y = R*np.sin(theta)+(ymin+ymax)/2
plt.plot(x, y, color='black', linewidth=2, linestyle='--',alpha = 0.3)

R1 = 22.5 #90秒區
x = R1*np.cos(theta)+(xmin+xmax)/2
y = R1*np.sin(theta)+(ymin+ymax)/2
plt.plot(x, y, color='black', linewidth=2, linestyle='--',alpha = 0.1)

# 設置 x y主刻度
ax.xaxis.set_major_locator(xmajorLocator) 
ax.yaxis.set_major_locator(ymajorLocator)
 
# 設置 x y次刻度
ax.xaxis.set_minor_locator(xminorLocator)
ax.yaxis.set_minor_locator(yminorLocator)

# x y座標軸使用次刻度
ax.xaxis.grid(True, which='minor') 
ax.yaxis.grid(True, which='minor') 

# xaxis-label 在上
ax.xaxis.set_ticks_position('top')

# 網格
plt.grid()

# 圖示
plt.legend(loc='lower left',fontsize=6)

# Y-axis
plt.gca().invert_yaxis()


plt.show()







參考資料

python 绘图---2D、3D散点图、折线图、曲面图
matplotlib 绘图可视化知识点整理
Matplotlib 教程
为python安装matplotlib模块
python – 在PyPlot中反转Y轴
matplotlib命令与格式:设置折线与点属性
python matplotlib绘图设置坐标轴刻度、文本
matplotlib 官方

在tkinter中使用matplotlib
用matplotlib画圆和圆环

↓↓↓ 連結到部落格方針與索引 ↓↓↓

Blog 使用方針與索引