How to do colored 2D grid with 3 arrays(如何用 3 个数组做彩色 2D 网格)
问题描述
我有三个长度相等的数组 x、y 和 z.x 和 y 数组是网格的 x 轴和 y 轴.z 数组将确定网格块的颜色.例如,
x = [10, 10, 10, 20, 20, 20, 30, 30, 30]y = [10, 20, 30, 10, 20, 30, 10, 20, 30]z = [100, 54, 32, 67, 71, 88, 100, 15, 29]
像这样制作 3D 绘图很容易
ax.plot_trisurf(x, y, z, cmap=cm.RdYlGn)
或
ax.bar3d(x, y, [0] * len(x), 100, 100, z, cmap=cm.RdYlGn)
但我正在寻找类似的东西
I have three arrays of equal length x, y, and z. The x and y arrays are the x-axis and y-axis for the grid. The z array will determine the color of the the grid block. For example,
x = [10, 10, 10, 20, 20, 20, 30, 30, 30]
y = [10, 20, 30, 10, 20, 30, 10, 20, 30]
z = [100, 54, 32, 67, 71, 88, 100, 15, 29]
It is easy to make 3D plots out of this like
ax.plot_trisurf(x, y, z, cmap=cm.RdYlGn)
or
ax.bar3d(x, y, [0] * len(x), 100, 100, z, cmap=cm.RdYlGn)
But I am looking for something like this
np.meshgrid
returns a tuple of two 2D arrays, which you can unpack directly
X,Y = np.meshgrid(x,y)
However, you don't need to those for an imshow plot. What you need and what you lack in your code is the 2D array of z
values. This would be the array to provide to imshow
.
img = plt.imshow(Z)
If you want to use meshgrid instead, you can use your X
and Y
values,
plt.pcolormesh(X,Y,Z)
Seeing the example data, you can use imshow:
x = [10, 10, 10, 20, 20, 20, 30, 30, 30]
y = [10, 20, 30, 10, 20, 30, 10, 20, 30]
z = [100, 54, 32, 67, 71, 88, 100, 15, 29]
import matplotlib.pyplot as plt
import numpy as np
z = np.array(z).reshape(3,3)
plt.imshow(z,extent=[5,35,5,35])
plt.show()
这篇关于如何用 3 个数组做彩色 2D 网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何用 3 个数组做彩色 2D 网格


基础教程推荐
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01