首页 > 解决方案 > Matplotlib 的 Spy、Imshow 和 Matshow 的问题

问题描述

我有以下使用 Python 的简单代码(我正在使用 Python 3)matplotlib.pyplotspyimshowmatshow.

from scipy import rand, floor
import matplotlib.pyplot as plt

Lx=30; Ly=20
p=0.4

r=rand(Lx,Ly) # Generates random numbers across the 2D lattice of size Lx*Ly
A = floor(r+p)

# Using spy 
plt.xlabel('$L_x$'); plt.ylabel('$L_y$')
plt.spy(A, origin='lower')
plt.title("Figure 1")
plt.savefig('fig1.png')

# Using imshow
plt.xlabel('$L_x$'); plt.ylabel('$L_y$')
plt.imshow(r,  cmap='rainbow', origin='lower', extent=[0,Lx,0,Ly])
plt.title("Figure 2")
plt.colorbar(orientation='horizontal')
plt.savefig('fig2.png')

# Using matshow
plt.xlabel('$L_x$'); plt.ylabel('$L_y$')
plt.matshow(r,  cmap='rainbow', origin='lower', extent=[0,Lx,0,Ly]) 
plt.colorbar(orientation='horizontal')
plt.title("Figure 3")
plt.savefig('fig3.png')


plt.show()

#Issues

# 1) plt.show() doesn't show spy-plot (Figure 1). 
# 2) Axes labels don't pass to matshow-plot (Figure 3).
# 3) spy-plot shows wrong-label ($L_x$ and $L_y$ are interchanged in Figure 2).
# 4) None of the plots set origin at 'lower', i.e. at the bottom-left position.

代码的结尾注释中提到了这 4 个问题。一个主要的是将原点设置在最左下角,并将标题放在底部。

可以解决这些问题或解释限制吗?

输出图像附在下面。

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

标签: pythonpython-3.xmatplotlibimshow

解决方案


推荐阅读