python - 改变情节的起源

标签 python matplotlib plot axis

我有一个包含 3 列的数据表,我想根据前两列在彩色二维图中绘制第三列。例如下表即

4.0 4.0 0.313660827978 
4.0 5.0 0.365348418405 
4.0 6.0 0.423733120134 
5.0 4.0 0.365348418405 
5.0 5.0 0.439599930621 
5.0 6.0 0.525083754405 
6.0 4.0 0.423733120134 
6.0 5.0 0.525083754405 
6.0 6.0 0.651536351379

我使用以下代码:

x,y,z = np.loadtxt('output_overlap.dat').T #Transposed for easier unpacking
nrows, ncols = final_step_j-1, final_step_k-1
grid = z.reshape((nrows, ncols))
plt.imshow(grid, extent=(x.min(), x.max(), y.max(), y.min()),
           interpolation='nearest', 
           cmap='binary')
fig1 = plt.gcf()
plt.colorbar()
plt.xlabel('m1')
plt.ylabel('m2')
plt.draw()
fig1.savefig('test.pdf', dpi=100)
close('all')

这给了我以下情节: https://dl.dropboxusercontent.com/u/31460244/test.png

这是正确的。现在,我的问题是:如何更改 Y Axis 上显示数据的顺序?我想在原点有点 (4,4)。

我试过改变

plt.imshow(grid, extent=(x.min(), x.max(), y.max(), y.min())

到:

plt.imshow(grid, extent=(x.min(), x.max(), y.min(), y.max())

它确实会更改网格中的数字,但不会更改实际数据。这不是解决方案。这里有人可以帮我吗?

最佳答案

范围只是将这些角坐标分配给数据,无论如何它不会改变底层数据的顺序。

imshow 对此有一个 origin 关键字,参见:

a = np.array([0.313660827978, 0.365348418405, 0.423733120134, 
              0.365348418405, 0.439599930621, 0.525083754405, 
              0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)

extent = [4,6,4,6]

fig, axs = plt.subplots(1,2)

axs[0].imshow(a, extent=extent, interpolation='none')
axs[1].imshow(a, origin='lower', extent=extent, interpolation='none')

enter image description here

您还可以考虑使用 np.flipudnp.fliplr 来镜像数组的 Axis 。但如果足够的话,我个人更喜欢使用 imshow 设置原点。

关于python - 改变情节的起源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19728445/

相关文章:

python - Matplotlib:与 plt.savefig() 和手动保存不同的 View

tensorflow - 属性错误 : module 'matplotlib.mlab' has no attribute 'bivariate_normal'

r - 使用 ggplot2 绘制样条函数

r - 无法让年份因子指向 x 轴

python - 使用 python 中的 PyQt5 按下按钮时更改 QML 中的标签

python - Sphinx 生成的 html 页面在 pull 到 NAS 驱动器 git 存储库时看起来不同

python - 如何在 Django 框架中正确制作自定义过滤器?

基于 Python 的异步工作流模块 : What is difference between celery workflow and luigi workflow?

python - 使用 Matplotlib 绘制二维数组

java - ImageJ 中一幅图中的 vector (Java)