Matplotlib底图颜色图与contourf和pcolormesh投掷错误

时间:2014-07-26 18:03:05

标签: python matplotlib compiler-errors map-projections matplotlib-basemap

我的程序从.txt文件中读取给出坐标和倾斜值的数据,将其插入指数回归以获得相应的倾斜误差值,然后通过地球地图上的坐标绘制误差图。我已成功使用Matplotlib Basemap映射不同大小的点,但我的目标是创建一个颜色图。我已经尝试过contourf和pcolormesh,但一直都会遇到错误,我怀疑我的问题在于我用于绘图的数组的维度。 latValues和longValues是我的坐标数组,errorValues表示强度。当我使用contourf调整我的代码时,我得到的两个错误是“IndexError:太多索引”和“MaskError:掩码和数据不兼容”。使用pcolormesh,我得到“ValueError:需要多于1个值才能解压缩。”我已经尝试过研究文档并将解决方案应用于其他人的类似问题,但没有任何改进。感谢您的帮助!

from mpl_toolkits.basemap import Basemap
import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt
from math import fabs, pow, e


'''put latitude and I value into an array'''

filename = raw_input('Enter filename: ')
txt = open(filename)

length = 1      #length of current line
curLine = 0     #entire line
curLat = 0      #latitude of current line
curLong = 0     #longitude
curI = 0        #inclination (dip angle)
valuesArray = []  #stores lat, long, and I values

while length != 0:   #while current line has data (i.e., isn't empty)
    curLine = txt.readline()
    length = len(curLine)
    if length != 0:
        curLat = float(curLine.split()[0])   #get first non-whitespace chunk and change type to float
        curLong = float(curLine.split()[1])  #get second non-whitespace chunk and change type to float
        curI = fabs(float(curLine.split()[-1]))    #get last non-whitespace chunk and change type to float. abs val
        valuesArray.append([curLat, curLong, curI])

'''append error values to valuesArray after calculate error by plugging in I values into equation'''

curError = 0    #current

def regression(inclin):  #this is the regression equation based off the table in the magnetometer manual used to calculate error values
    curError = 1.505690874240565 * pow(10, -13) * pow(e, (0.348649 * inclin))
    return curError

counter = 0
while counter < len(valuesArray):
    valuesArray[counter].append(regression(valuesArray[counter][2])) #append error value to valuesArray
    counter = counter + 1

latValues = []     #list of latitude values
longValues = []    #list of longitude values
inclinValues = []  #list of inclination values
errorValues = []   #list of error values
counter2 = 0

while counter2 < len(valuesArray):    #create arrays for each value (lat, long, inclin, and error)
    latValues.append(valuesArray[counter2][0])
    longValues.append(valuesArray[counter2][1])
    inclinValues.append(valuesArray[counter2][2])
    errorValues.append(valuesArray[counter2][3])
    counter2 = counter2 + 1

#change lists to arrays to numpy arrays if needed
##latValues = np.asarray(latValues)
##longValues = np.asarray(longValues)
##inclinValues = np.asarray(inclinValues)
##errorValues = np.asarray(errorValues)
##latValues = np.frombuffer(latValues)
##longValues = np.frombuffer(longValues)
##inclinValues = np.frombuffer(inclinValues)
##errorValues = np.frombuffer(errorValues)    

'''set up south polar stereographic basemap and try to graph'''

m = Basemap(projection='spstere', boundinglat=-35, lon_0=90, resolution='l') #boundinglat determines how graph is zoomed in
m.drawcoastlines()
m.fillcontinents(color='brown', lake_color='blue')

x2d, y2d = np.meshgrid(longValues, latValues)
x, y = m(x2d, y2d)
##plt.pcolormesh(x, y, errorValues, cmap=plt.cm.jet)  #if i want to try to get pcolormesh to work
m.contour(x, y, errorValues, levels = range(-180, 360, 30), colors = 'orange')

m.drawparallels(np.arange(-80.,81.,20.))
m.drawmeridians(np.arange(-180.,181.,20.))
m.drawmapboundary(fill_color = 'blue')

plt.title("South Polar Stereographic Projection")
plt.show()

0 个答案:

没有答案