以前有使用過用 gnuplot 來繪製校務系統的座位成績圖,改用 python 搭配 matplotlib 來繪製新的 3d 圖形
程式碼:
import sys
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
with open("grade_data/sm2-1.txt", "r") as f:
mydata = f.readlines()
x = []
y = []
z = []
for myline in mydata:
if myline[0] != '#':
tmp = myline.split()
x.append(int(tmp[5]))
y.append(int(tmp[6]))
z.append(float(tmp[4]))
bottom = np.zeros_like(z)
width = depth = 0.5
fig = plt.figure()
cmap = cm.get_cmap('CMRmap')
max_h = np.max(z)
min_h = np.min(z)
co = [cmap((k - min_h)/max_h) for k in z]
ax = fig.add_subplot(projection='3d')
ax.bar3d(x, y, bottom, width, depth, z, shade=True, color=co)
ax.grid(True)
plt.xlabel("X")
plt.ylabel("Y")
ax.set_title("n = %s , mean = %s, std = %s" %(len(z), round(np.mean(z),2), round(np.std(z), 2)))
plt.show()
程式碼:
import sys
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
with open("grade_data/sm2-1.txt", "r") as f:
mydata = f.readlines()
x = []
y = []
z = []
for myline in mydata:
if myline[0] != '#':
tmp = myline.split()
x.append(int(tmp[5]))
y.append(int(tmp[6]))
z.append(float(tmp[4]))
bottom = np.zeros_like(z)
width = depth = 0.5
fig = plt.figure()
cmap = cm.get_cmap('CMRmap')
max_h = np.max(z)
min_h = np.min(z)
co = [cmap((k - min_h)/max_h) for k in z]
ax = fig.add_subplot(projection='3d')
ax.bar3d(x, y, bottom, width, depth, z, shade=True, color=co)
ax.grid(True)
plt.xlabel("X")
plt.ylabel("Y")
ax.set_title("n = %s , mean = %s, std = %s" %(len(z), round(np.mean(z),2), round(np.std(z), 2)))
plt.show()
留言
張貼留言