import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
# Load Data
radData = np.loadtxt('rad.txt')
# x
xData = list(radData[:,0])
x = list(set(xData))
x.sort(key=xData.index)
# y
yData = list(radData[:,1])
y = list(set(yData))
y.sort(key=yData.index)
# to X and Y
X, Y = np.meshgrid(y, x)
# z
z = radData[:,2]
Z = np.array(z)
Z = Z.reshape((len(x), len(y)))
# width and height
width = max(x) - min(x)
height = max(y) - min(y)
fig = plt.figure(figsize=(width,height))
ax = fig.add_subplot(1, 1, 1)
# 填充等高线的颜色, 9是等高线分为几部分
plt.contourf(Y, X, Z, 9, alpha = 0.75, cmap = plt.cm.jet)
# 绘制等高线
C = plt.contour(Y, X, Z, 9, colors = 'black')
# 绘制等高线数据
plt.clabel(C, inline = True, fontsize = 10)
# 去除坐标轴
plt.axis('off')
plt.tick_params(axis='both', left='off', top='off', right='off', bottom='off', labelleft='off', labeltop='off',
labelright='off', labelbottom='off')
#pad_inches设置为-0.05是为了剪去白边
plt.savefig('rad.png', bbox_inches='tight', dpi=90, pad_inches=-0.05)
plt.show()
数据格式(rad.txt)
行坐标<SP>列坐标<SP>值
例如:
-15.000 -7.500 0.0000
-15.000 -7.400 0.0000
-15.000 -7.300 0.0000
-15.000 -7.200 0.0000
-15.000 -7.100 0.0000
-15.000 -7.000 0.0000
-15.000 -6.900 1.0000
-15.000 -6.800 1.0000
-15.000 -6.700 1.0000