python - 将 1-D x、y、z 向量转换为 2D 网格
问题描述
我有最初位于 3 列数据中的空间数据集 - x、y、z
请运行此代码并查看数据的外观。
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data = pd.DataFrame(np.array([[50. , 50. , 0.3],
[55. , 25. , -1.5],
[ 6.6, 47.4, 0.1],
[35. , 34.9, 1. ],
[32.7, 56.3, 1.9]]), columns=['x', 'y', 'z'])
x = data['x']
y = data['y']
z = data['z']
def plot_sparse(x, y, z, ax, dist_max, **kwargs):
assert len(x) == len(y) == len(z), "x, y, and z must have the same dimension"
sc = ax.scatter(x, y, c=z, **kwargs)
ax.set_aspect("equal")
ax.set_xlim(0, dist_max)
ax.set_ylim(0, dist_max)
return sc
fig, ax = plt.subplots()
sc = plot_sparse(x, y, z, ax=ax, dist_max=80)
fig.colorbar(sc)
我想复制使用 meshgrid 生成的图形:
[X, Y] = np.meshgrid(x, y)
fig, ax = plt.subplots()
sc = ax.scatter(x, y)
ax.set_aspect("equal")
ax.set_xlim(0, 80)
ax.set_ylim(0, 80)
但我不知道如何将 z 轴的值嵌入网格中。上面使用 meshgrid 的代码在 2D 曲面图上不会有颜色条,但我想使用 meshgrid 数据类型制作颜色条。
我怎样才能做到这一点?
解决方案
推荐阅读
- c++ - 复制先例值二维动态数组
- r - 创建列,告诉前两列之间的数字来源
- java - 记录似乎在顺序数据库操作(Spring Data、Hibernate)之间被锁定
- angular - MatInput 值在使用 value="x" 后显示空输入字段
- sql - Oracle SQL 查询在最大日期获取值
- python - 带有常规列表的 Heapop 和 Heappush(非堆化列表)
- python - 电报机器人错误。TypeError:“NoneType”对象不可订阅
- java - 问题:返回 ResponseEntity 类型的 Flux
- git - 本地 Git 服务器——突然无法推送/拉取/克隆
- amazon-web-services - AWS SQS 服务可以触发 API Gateway 代理吗?