python - 在 matplotlib 曲面绘图时,对象不能广播到单个形状
问题描述
我正在使用 X=lat, Y=long 和 Z=depth(Elevation)和 csv绘制一个 3D 曲面图。我很确定所有变量的长度都相同 43 但我收到以下错误,程序指的是什么值?
ValueError: shape mismatch: objects cannot be broadcast to a single shape
.
这是我的python片段。
import matplotlib.pyplot as plt
%matplotlib inline
import plotly.graph_objects as go
import pandas as pd
import numpy as np
# Read data from a csv
z_data = pd.read_csv('bathy_bedford.csv')
z = z_data.values
sh_0, sh_1 = z.shape
x, y = np.linspace(44.66875, 44.74791667, sh_0), np.linspace(-63.69791667, -63.52708333, sh_0)
#plotting
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(1,1,1, projection='3d')
ax.plot_surface(x, y, z, rstride=4, cstride=4, alpha=0.25)
cset = ax.contour(x, y, z, zdir='z', offset=-np.pi, cmap=matplotlib.cm.coolwarm)
cset = ax.contour(x, y, z, zdir='x', offset=-np.pi, cmap=matplotlib.cm.coolwarm)
cset = ax.contour(x, y, z, zdir='y', offset=3*np.pi, cmap=matplotlib.cm.coolwarm)
ax.set_xlim3d(-np.pi, 2*np.pi);
ax.set_ylim3d(0, 3*np.pi);
ax.set_zlim3d(-np.pi, 2*np.pi);
这是变量长度的输出:
len(x)
43
len(y)
43
len(z)
43
解决方案
进口
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.cm import coolwarm
从 csv 加载数据
df = pd.read_csv('bathy_bedford.csv', index_col=0)
# column names from str to float
df.columns = [float(x) for x in df.columns]
# df.head()
44.668750 44.672917 44.677083 44.681250 44.685417 44.689583 44.693750 44.697917 44.702083 44.706250 44.710417 44.714583 44.718750 44.722917 44.727083 44.731250 44.735417 44.739583 44.743750 44.747917
-63.697917 76.949219 77.085938 94.507813 109.914060 111.292970 88.378906 51.730469 47.687500 59.089844 53.457031 46.628906 39.363281 44.792969 52.582031 41.074219 32.304688 31.945313 37.171875 31.265625 35.207031
-63.693750 74.859375 75.480469 88.718750 104.511720 102.984380 72.757813 51.261719 57.562500 68.406250 64.949219 46.140625 35.457031 41.566406 46.582031 44.464844 43.144531 48.738281 41.949219 28.066406 41.425781
-63.689583 76.234375 75.566406 80.800781 85.156250 76.046875 55.621094 57.980469 73.234375 78.527344 77.343750 51.320313 30.578125 33.000000 44.218750 57.890625 63.746094 67.226563 60.589844 43.121094 37.597656
-63.685417 78.855469 77.718750 70.976563 61.859375 52.851563 54.816406 72.570313 84.394531 86.800781 80.457031 56.378906 30.882813 33.000000 52.328125 71.847656 84.273438 86.675781 79.574219 53.785156 28.941406
-63.681250 75.035156 67.761719 68.031250 65.218750 56.160156 68.144531 88.738281 93.468750 92.445313 79.990234 49.884766 24.603516 39.861328 64.859375 78.960938 86.257813 86.726563 84.812500 65.078125 40.824219
指定x
、y
和z
- 用于
numpy.meshgrid
正确塑造x
&y
匹配z
。 - 的
shape
,x
,y
&z
将分别是(43, 20)
。
x = df.columns
y = df.index
# make x & y match the shape of z
x, y = np.meshgrid(x, y)
z = df.values
绘制数据
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, rstride=4, cstride=4, alpha=0.25)
cset = ax.contour(x, y, z, zdir='z', offset=-np.pi, cmap=coolwarm)
cset = ax.contour(x, y, z, zdir='x', offset=-np.pi, cmap=coolwarm)
cset = ax.contour(x, y, z, zdir='y', offset=3*np.pi, cmap=coolwarm)
plt.show()
推荐阅读
- reactjs - 如何将 gatsby-plugin-google-gtag 与 Gatsby.js 一起使用?
- r - K-means 聚类的内存问题
- mysql - 如何检查 mysql 查询中的 2 个唯一字段
- ios - 从集合视图控制器调用时集合视图标题中的 UIButton 不起作用,如何解决此问题?
- java - 在本地禁用领事配置
- java - 如何在intellij中纠正java多行中单个语句的缩进
- mysql - Zend Framework 2 Db getColumns
- javascript - Javascript:类的第二个实例覆盖第一个
- python - 子进程管道仅在运行脚本时损坏
- r - 在ggplot2中绘制一个带网格的正方形