简短版本:
我有一个10x10的numpy数组,其等高线图(用Analyst
绘制)看起来像这样
现在,我希望它看起来像这样 - 假设绘图在X和Y轴上是对称的。
长版
我有一个10x10 numpy数组pyplot.contourf
作为z
和x
的函数。其中y
。这是我试过的
x=y=np.arange(0.002,0.022,0.002)
我使用上面的代码
得到以下图表和
理想情况下,我还希望通过插值绘制来填充原点的不连续性。但对于初学者来说,想要获得正确的方向。任何帮助是极大的赞赏。
答案 0 :(得分:3)
您的问题是,即使您否定x
和y
,他们的订单也会保持不变,因此,如果您使用了x
,则会从-0.002
转到{{ 1}},这意味着在绘图过程中翻转的-0.022
会被翻转。要实现您的目标,您可以执行以下操作:
z
如果您刚刚连接#either don't flip z
plt.figure()
plt.contourf(x,y,z)
plt.contourf(-x,y,z)
plt.contourf(x,-y,z)
plt.contourf(-x,-y,z)
plt.show()
#or reverse also -x and -y:
plt.figure()
plt.contourf(x,y,z)
plt.contourf(-x[::-1],y,np.fliplr(z))
plt.contourf(x,-y[::-1],np.flipud(z))
plt.contourf(-x[::-1],-y[::-1],np.flipud(np.fliplr(z)))
plt.show()
和翻转z
,那么一切都会按预期工作。 z
负责插值本身。
plt.contourf
答案 1 :(得分:2)
将数组fliplr
的{{1}}和flipud
的结果合并到一个新的双倍大小的数组z
,然后绘制它。根据您的第一个数字,您必须使用nan值跳过区间(-0.002; +0.002)中的x和y:
zz
import numpy as np
import matplotlib.pyplot as plt
z=np.array([[ 2.08273679, -0.06591932, -1.14525488, -1.49923222, -1.74361248,
-1.81418446, -1.90115591, -1.94329043, -1.93130228, -1.96064259],
[ 0.20180514, -0.94522815, -1.34635828, -1.58844515, -1.7528935 ,
-1.84438752, -1.86257547, -1.9439332 , -1.99009407, -1.94829146],
[-1.09749238, -1.48234452, -1.64234357, -1.75344742, -1.83019763,
-1.88547473, -1.92958533, -1.940775 , -1.95535063, -1.9629588 ],
[-1.62892483, -1.70176401, -1.76263555, -1.84966414, -1.87139241,
-1.91879916, -1.90796703, -1.96632612, -1.95794984, -1.94585536],
[-1.71551518, -1.91806287, -1.86999609, -1.90800839, -1.92515012,
-1.93386969, -1.96487487, -1.95405297, -1.97032435, -1.96087146],
[-1.81904322, -1.94790171, -2. , -1.96932249, -1.91842475,
-1.98101775, -1.98521938, -1.97618539, -1.95892852, -2.01410874],
[-1.8138236 , -1.90877811, -1.93966404, -1.98406259, -1.95253807,
-1.95867436, -1.96679456, -2.01126218, -1.99885932, -1.99369292],
[-1.9927308 , -1.97658099, -1.91586737, -1.96813381, -1.98416011,
-1.98639893, -1.99997964, -1.99746813, -1.98126505, -1.97767361],
[-1.96406473, -1.92609437, -1.99171257, -1.94687523, -1.9823819 ,
-1.97786533, -2.02323228, -1.98559114, -1.99172681, -2.00881064],
[-1.92470024, -1.99537152, -1.99419303, -1.97261023, -1.9673841 ,
-1.98801505, -2.02412735, -2.01394008, -2.01956817, -2.04963448]])
x=y=np.linspace(-0.020,0.020,21)
zz = np.empty((21,21)); zz[:,:] = np.nan
zz[11:,11:] = z
zz[11:,:10] = np.fliplr(z)
zz[:10,:] = np.flipud(zz[11:,:])
plt.figure()
plt.contourf(x,y,zz)
plt.show()