如何调整我的展示大小? [英] How to size my imshow?

查看:48
本文介绍了如何调整我的展示大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码生成了一个二维强度矩阵:

  H,x_e,y_e = np.histogram2d(test_y,test_x,bins =(y_e,x_e))

x_e 和 y_e 的值为:

  x_e数组([ 0.05 , 0.0530303 , 0.05606061, 0.05909091, 0.06212121,0.06515152, 0.06818182, 0.07121212, 0.07424242, 0.07727273,0.08030303, 0.08333333, 0.08636364, 0.08939394, 0.09242424,0.09545455, 0.09848485, 0.10151515, 0.10454545, 0.10757576,0.11060606、0.11363636、0.11666667、0.11969697、0.12272727、0.12575758、0.12878788、0.13181818、0.13484848、0.13787879,0.14090909、0.14393939、0.1469697、0.15、0.1530303、0.15606061、0.15909091、0.16212121、0.16515152、0.16818182、0.17121212、0.17424242、0.17727273、0.18030303、0.18333333,0.18636364、0.18939394、0.19242424、0.19545455、0.19848485,0.20151515、0.20454545、0.20757576、0.21060606、0.21363636,0.21666667、0.21969697、0.22272727、0.22575758、0.22878788、0.23181818, 0.23484848, 0.23787879, 0.24090909, 0.24393939,0.2469697、0.25、0.2530303、0.25606061、0.25909091,0.26212121、0.26515152、0.26818182、0.27121212、0.27424242,0.27727273, 0.28030303, 0.28333333, 0.28636364, 0.28939394,0.29242424、0.29545455、0.29848485、0.30151515、0.30454545,0.30757576, 0.31060606, 0.31363636, 0.31666667, 0.31969697,0.32272727, 0.32575758, 0.32878788, 0.33181818, 0.33484848,0.33787879, 0.34090909, 0.34393939, 0.3469697, 0.35 ])y_earray([0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])

我似乎无法使用以下代码来控制我绘制的输出的形状:

fig = plt.figure(figsize=(10, 10))ax = fig.add_subplot(111)ax.set_title(feature_of_interest)im = plt.imshow(H,插值='最近',原点='低',范围= [y_e [0],y_e [-1],x_e [0],x_e [-1]])

这给了我非常紧凑的输出,我什么都看不到:

如何调整参数以获得更好的宽高比?

这是我到目前为止尝试过的:

  • 使用 extent 参数.这会改变形状,但不是以可预测的方式.这也使轴标签不正确.
  • 更改 figsize 参数.这似乎没有任何影响.

解决方案

您可以设置

I generated a 2d intensity matrix with the following code:

H, x_e, y_e = np.histogram2d(test_y, test_x, bins=(y_e, x_e))

The values of x_e and y_e are:

x_e
array([ 0.05      ,  0.0530303 ,  0.05606061,  0.05909091,  0.06212121,
        0.06515152,  0.06818182,  0.07121212,  0.07424242,  0.07727273,
        0.08030303,  0.08333333,  0.08636364,  0.08939394,  0.09242424,
        0.09545455,  0.09848485,  0.10151515,  0.10454545,  0.10757576,
        0.11060606,  0.11363636,  0.11666667,  0.11969697,  0.12272727,
        0.12575758,  0.12878788,  0.13181818,  0.13484848,  0.13787879,
        0.14090909,  0.14393939,  0.1469697 ,  0.15      ,  0.1530303 ,
        0.15606061,  0.15909091,  0.16212121,  0.16515152,  0.16818182,
        0.17121212,  0.17424242,  0.17727273,  0.18030303,  0.18333333,
        0.18636364,  0.18939394,  0.19242424,  0.19545455,  0.19848485,
        0.20151515,  0.20454545,  0.20757576,  0.21060606,  0.21363636,
        0.21666667,  0.21969697,  0.22272727,  0.22575758,  0.22878788,
        0.23181818,  0.23484848,  0.23787879,  0.24090909,  0.24393939,
        0.2469697 ,  0.25      ,  0.2530303 ,  0.25606061,  0.25909091,
        0.26212121,  0.26515152,  0.26818182,  0.27121212,  0.27424242,
        0.27727273,  0.28030303,  0.28333333,  0.28636364,  0.28939394,
        0.29242424,  0.29545455,  0.29848485,  0.30151515,  0.30454545,
        0.30757576,  0.31060606,  0.31363636,  0.31666667,  0.31969697,
        0.32272727,  0.32575758,  0.32878788,  0.33181818,  0.33484848,
        0.33787879,  0.34090909,  0.34393939,  0.3469697 ,  0.35      ])
y_e
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])

I cannot seem to achieve control over the shape of my plotted output with this code:

fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111)
ax.set_title(feature_of_interest)
im = plt.imshow(H, interpolation='nearest', origin='low',
                extent=[y_e[0], y_e[-1], x_e[0], x_e[-1]])

This gives me a very squashed output where I can't see anything:

How can I adjust the parameters to get a better aspect ratio?

Here's what I've tried so far:

  • playing around with the extent parameter. This changes the shape, but not in a predictable way. Also that makes the axis labels incorrect.
  • changing the figsize parameter. This doesn't seem to have any effect.

解决方案

You can set the aspect ratio of the axes directly. This is independent of the figure size. Here's an example:

import numpy as np
from matplotlib import pyplot as plt

data = np.random.rand(5, 100)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.imshow(data, interpolation='nearest')
ax.set_aspect(5)

plt.show()

这篇关于如何调整我的展示大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆