【发布时间】:2015-08-16 22:21:18
【问题描述】:
我正在使用 matplotlib 在 python 中绘制一个二维数组,但在格式化刻度线时遇到了问题。因此,首先,我的数据目前被组织为具有(海拔,纬度)的二维数组。我正在绘制作为高度和纬度函数的电子密度值(基本上是特定时间的纵向切片)。
我想以 30 度的间隔标记从 -90 到 90 度的 x 轴,并用另一个高程数组标记 y 值(每个模型运行都有不同的高程值,因此我无法手动分配任意高程)。我有一个带有纬度值的数组,另一个带有高程值的数组都是一维数组。
这是我的代码:
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
#load the netcdf file into a variable
mar120="C:/Users/WillEvo/Desktop/sec_giptie_cpl_mar_120.nc"
#grab the data into a new variable
fh=Dataset(mar120,mode="r")
#assign model variable contents to python variables
lons=fh.variables['lon'][:]
lats=fh.variables['lat'][:]
var1=fh.variables['UN'][:]
#specifying which time and elevation to map
ionst=var1[0,:,:,21]
ionst=ionst[0:len(ionst)-1]
#close the netCDF file
fh.close()
#Set the figure, size, and resolution
plt.figure(figsize=(8,6), dpi=100, facecolor='white')
plt.subplot(1,1,1)
plt.imshow(ionst, origin='lower', interpolation='spline16')
plt.xticks([-90, -60, -30, 0, 30, 60, 90])
plt.show()
如果我不包含 plt.xticks 参数,我会得到以下良好的图像但错误的刻度标签:
如果我包含 plt.xticks 参数,我会得到以下信息:
我该如何解决这个问题?我希望数据跟随轴的变化(但要准确)。我还需要对 y 轴执行此操作,但无需手动输入值,而是提供一组值。谢谢
【问题讨论】:
-
这个条目有帮助吗? stackoverflow.com/questions/18696122/…
标签: python numpy multidimensional-array matplotlib axes