使用自定义单元格宽度和高度基于 Numpy 2d 数组创建 NonUniformImage [英] Create NonUniformImage based on Numpy 2d array with custom cell width and height

查看:93
本文介绍了使用自定义单元格宽度和高度基于 Numpy 2d 数组创建 NonUniformImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 2d Numpy 数组的 Matplotlib 制作热图",并找到了 NonUniformImage 选项 此处.下面是一个简单的单一版本.不幸的是,我不能不改变y而改变x.pyhton 提示的错误是:Axes don't match array shape

I'm trying to make a "heatmap" with Matplotlib of a 2d Numpy Array and found the NonUniformImage option here. Below is a simple single version. Unfortunately I'm not able to change the x without changing the y. The error that pyhton Propmpts is: Axes don't match array shape

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import NonUniformImage
from matplotlib import cm

x = np.arange(6)
y = np.arange(6)
z = x[:, np.newaxis].dot(y[:, np.newaxis].transpose())

fig, ax = plt.subplots()
im = NonUniformImage(ax, interpolation='nearest', extent=(0, len(x)-1, 0, len(y)-1))
im.set_data(x, y, z)
ax.images.append(im)
ax.set_xlim(0, len(x)-1)
ax.set_ylim(0, len(y)-1)

我的目标是基于带有自定义网格单元"的 Numpy 2d 数组绘制地图.这意味着我可以分配具有不规则序列的x和y(例如 np.array([0.5,3,8,15,15.4]).我知道分配的x和y代表中心一个单元格,所以我想解决的最后一件事是该图是完全可见的(如果可能的话).

My goal is to plot a map based on a Numpy 2d array with 'custom Grid cells'. That means that I can assign x and y with irregular series (e.g. np.array([0.5,3,8,15,15.4]). I know the x and y that are assign represent the center of a cell so the last thing I would like to solve is that the plot is completely visible (if that is possible).

如何制作具有不同约束条件(不是正方形)且自定义宽度和高度取决于两个列表的图像?

How can I make a image that has a different constraint (not square), with custom width and height depending on two lists?

推荐答案

我想您在定义 z 时出错.可能是您追求的是

I suppose you made a mistake defining z. Possibly what you're after is

x = np.arange(6)
y = np.arange(8)
z = x[np.newaxis,:] * y[:, np.newaxis]
print(z.shape)

形状为(8,6).

这篇关于使用自定义单元格宽度和高度基于 Numpy 2d 数组创建 NonUniformImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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