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 网格


基础教程推荐
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 包装空间模型 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01