绘制非方阵的轮廓图
我正在尝试用 matplotlib
制作一个大数组的3D投影图(等高线图),结果看起来是这样的:
我觉得这可能和定义x轴和y轴有关。我写的绘图代码如下,数据可以在这里找到:
def plotLikelihood(array,m,c):
xi, yi = np.linspace(m.min(), m.max(), 100), np.linspace(c.min(), c.max(), 100)
# Interpolate
rbf = scipy.interpolate.interp2d(m, c,array , kind='linear')
zi = rbf(xi, yi)
fig, ax = plt.subplots()
divider = make_axes_locatable(ax)
im = ax.imshow(zi, vmin=array.min(), vmax=array.max(), origin='lower',
extent=[m.min(), m.max(), c.min(),c.max()])
ax.set_xlabel(r"$Mass$")
ax.set_ylabel(r"$Concentration$")
ax.xaxis.set_label_position('top')
ax.xaxis.set_tick_params(labeltop='on')
cax = divider.append_axes("right", size="5%", pad=0.05)
cbar = fig.colorbar(im,cax=cax, ticks=list(np.linspace(array.max(), array.min(),20)),format='$%.2f$')
cbar.ax.tick_params(labelsize=8)
plt.savefig('Likelihood2d_MC_NoShapeNoise.pdf', transparent=True, bbox_inches='tight', pad_inches=0)
plt.close()
这个函数的输入是m和c,具体如下:
m = np.linspace(0.01, 10, 10000)
Mass=1e15*m
Conc = np.linspace(2, 12, 1000)
likelihood=np.savetxt("Likelihood2d_MC_NoShapeNoise.txt")
plotLikelihood(likelihood,Mass,Conc)
1 个回答
1
在imshow
中添加一个关键字参数aspect='auto'
(或者在之后使用ax.set_aspect('auto')
),这样你就不用担心了……
(有一个小小的请求:你的示例代码几乎是完整的,也差不多能运行。不过缺少了一些导入的部分,最后的代码片段还有一些拼写错误。如果把248 MB的文件换成np.random.random((1000,10000))
,你也能重现同样的现象。这些小问题会让调试变得简单一些。)