gpt4 book ai didi

python - 在不使用 xticks 的情况下设置二维等高线图的轴值

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:44 25 4
gpt4 key购买 nike

我正在尝试设置使用 contour() 生成的绘图的 x 轴和 y 轴的值,但我目前无法从轴上读取所需的特定值。

fid_list = []
for fidN in arange(frames):
offset = fidN * fid_pts
current_fid = cmplx_data[offset:offset+fid_pts]
fid_list.append(current_fid)

fid_mat = fid_list
jres_spec = abs(fftshift(fft2(fid_mat)))
max_val = jres_spec.max()/15
min_val = max_val*0.15
steps = 40
figure()
CS=contour(jres_spec,arange(min_val,max_val,(max_val-min_val)/steps))
show()

生成这样的图

enter image description here以前我一直使用 xticks 和 yticks 来设置轴的值,但现在绘图上的确切位置变得很重要,因此能够读取轴上的值将非常有帮助,而我不能用 x/yticks.

绘制一维光谱时,我使用以下公式来读取 x 轴

bins = arange(828, -196, -1) #change this so that 0 value occurs at value it's meant to
x = (2000 * bins / 1024.0)/128.0
plot(x, fftshift(fft(fid_list[0])))
plt.gca().invert_xaxis()
show()

并且会类似地将其用于我的 2D 等高线图的 y 轴

ybins = arange(15, -15, -1)
y = ybins * ((1/(15*10^(-3)))/ 30.0)

但是我无法将其集成到我的代码中...

我试过用这样的东西

ybins = arange(15, -15, -1)
y = ybins * ((1/(15*10^(-3)))/ 30.0)
xbins = arange(828, -196, -1)
x = (2000 * xbins / 1024.0)/128.0

fid_mat = fid_list
jres_spec = abs(fftshift(fft2(fid_mat)))
max_val = jres_spec.max()/15
min_val = max_val*0.15
steps = 40
figure()
CS=contour((x, y, jres_spec),arange(min_val,max_val,(max_val-min_val)/steps))
show()

哪个返回了

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/dominicc/<ipython-input-34-28b34c6c069d> in <module>()
7 bins = arange(828, -196, -1) #change this so that 0 value occurs at value it's meant to
8 x = (2000 * bins / 1024.0)/128.0
----> 9 CS=contour((x_list, jres_spec),arange(min_val,max_val,(max_val-min_val)/steps))
10 show()

/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in contour(*args, **kwargs)
2195 ax.hold(hold)
2196 try:
-> 2197 ret = ax.contour(*args, **kwargs)
2198 draw_if_interactive()
2199 finally:

/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in contour(self, *args, **kwargs)
7379 if not self._hold: self.cla()
7380 kwargs['filled'] = False
-> 7381 return mcontour.QuadContourSet(self, *args, **kwargs)
7382 contour.__doc__ = mcontour.QuadContourSet.contour_doc
7383

/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
1110 are described in QuadContourSet.contour_doc.
1111 """
-> 1112 ContourSet.__init__(self, ax, *args, **kwargs)
1113
1114 def _process_args(self, *args, **kwargs):

/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
701 if self.origin == 'image': self.origin = mpl.rcParams['image.origin']
702
--> 703 self._process_args(*args, **kwargs)
704 self._process_levels()
705

/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in _process_args(self, *args, **kwargs)
1123 self.zmax = args[0].zmax
1124 else:
-> 1125 x, y, z = self._contour_args(args, kwargs)
1126
1127 x0 = ma.minimum(x)

/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in _contour_args(self, args, kwargs)
1167 if Nargs <= 2:
1168 z = ma.asarray(args[0], dtype=np.float64)
-> 1169 x, y = self._initialize_x_y(z)
1170 args = args[1:]
1171 elif Nargs <=4:

/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in _initialize_x_y(self, z)
1230 '''
1231 if z.ndim != 2:
-> 1232 raise TypeError("Input must be a 2D array.")
1233 else:
1234 Ny, Nx = z.shape

TypeError: Input must be a 2D array.

我现在正在努力想出其他方法来做到这一点。

有什么想法/建议吗?

最佳答案

您可以通过在调用 contour 时去掉额外的括号来消除错误:

CS=contour(x, y, jres_spec,arange(min_val,max_val,(max_val-min_val)/steps))

如果这没有给您想要的情节,请尝试使用 xlimylim 直接设置轴限制。

关于python - 在不使用 xticks 的情况下设置二维等高线图的轴值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17792151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com