You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

mplot3d绘制3D曲面时数据被截断至50组的问题求助

mplot3d绘制3D曲面时数据被截断至前49组的问题

我查阅了相关文档,尝试通过设置rstride和rcount参数让mplot3d绘制超过50组x、y、z数据的3D曲面,但未得到降采样效果,反而出现数据截断——绘制的曲面仅包含前49组xyz点,无法实现200组数据的曲面绘制。

已尝试的操作

  • 使用plotly,问题依旧存在;
  • 将rstride/rcount设为高值(200)或低值(1),调整meshgrid等多种操作,最终仍仅绘制前49组点;
  • 可以通过matplotlib的其他模块绘制包含所有点的3D散点图,但无法绘制曲面或三角曲面。

关键信息

  • 已知mplot3d默认限制为50,超过会降采样,但实际情况是第49组之后的数据被直接丢弃;
  • 已检查DataFrame,确认所有200行数据均存在,但绘图始终在第49组后截断。

相关代码

import pandas as pd
import matplotlib.pyplot as plt
from google.colab import files
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np
from scipy.interpolate import griddata
import io

# Upload the Excel file
uploaded = files.upload()

# Get the file name
file_name = list(uploaded.keys())[0]

# Read the Excel file with all sheets into a dictionary of DataFrames
xls = pd.ExcelFile(io.BytesIO(uploaded[file_name]))
sheet_names = xls.sheet_names
dataframes = {sheet_name: pd.read_excel(xls, sheet_name=sheet_name) for sheet_name in sheet_names}

# Specify the columns to plot
x_column = 'Col_1'
y_column = 'Col_2'
z_column = 'z_values'

# Find the min and max Z values across all sheets
min_z = float('inf')
max_z = float('-inf')
for sheet_name in sheet_names:
    df = dataframes[sheet_name]
    if z_column in df.columns:
        min_z = min(min_z, df[z_column].min())
        max_z = max(max_z, df[z_column].max())

# Find the min and max values for the colormap across all sheets
min_value = float('inf')
max_value = float('-inf')
for sheet_name in sheet_names:
    df = dataframes[sheet_name]
    if z_column in df.columns:  # Assuming z_column determines color
        min_value = min(min_value, df[z_column].min())
        max_value = max(max_value, df[z_column].max())

# Create a figure and subplots for each sheet
fig = plt.figure(figsize=(15, 10))
num_sheets = len(sheet_names)
num_rows = (num_sheets + 2) // 3
num_cols = min(num_sheets, 3)

for i, sheet_name in enumerate(sheet_names):
    df = dataframes[sheet_name]
    if x_column not in df.columns or y_column not in df.columns or z_column not in df.columns:
        print(f"Error: Columns '{x_column}', '{y_column}', or '{z_column}' not found in sheet '{sheet_name}'. Skipping.")
        continue

    ax = fig.add_subplot(num_rows, num_cols, i + 1, projection='3d')
    xi = np.linspace(min(df[x_column]), max(df[x_column]), 100)
    yi = np.linspace(min(df[y_column]), max(df[y_column]), 100)
    X, Y = np.meshgrid(xi, yi)
    Z = griddata((df[x_column], df[y_column]), df[z_column], (X, Y), method='cubic')
    
    
    surf = ax.plot_surface(X, Y, Z, cmap=cm.viridis, linewidth=0, antialiased=False, vmin=min_value, vmax=max_value)
    #surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='viridis') to prevent downsampling
    
    ax.set_xlabel(x_column)
    ax.set_ylabel(y_column)
    ax.set_zlabel(z_column)
    ax.set_title(f'Sheet: {sheet_name}')
    ax.set_zlim(min_z, max_z)


# Add a single colorbar for all plots
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(surf, cax=cbar_ax)

plt.show()

测试数据

Col_1   Col_2   z_values
1   1   50
2   2   51
3   3   52
4   3   53
5   2   50
1   1   51
2   1   52
3   2   53
4   3   50
5   3   51
1   2   52
2   1   53
3   1   50
4   2   51
5   3   52
1   3   53
2   2   50
3   1   51
4   1   52
5   2   53
1   3   50
2   3   51
3   2   52
4   1   53
5   1   50
1   2   51
2   3   52
3   3   53
4   2   50
5   1   51
1   1   52
2   2   53
3   3   50
4   3   51
5   2   52
1   1   53
2   1   50
3   2   51
4   3   52
5   3   53
1   2   50
2   1   51
3   1   52
4   2   53
5   3   50
1   3   51
2   2   52
3   1   53
4   1   50
5   2   51
1   3   52
2   3   153
3   2   50
4   1   51
5   1   52
1   2   53
2   3   50
3   3   51
4   2   52
5   1   53
1   1   50
2   2   51
3   3   52
4   3   53
5   2   50
1   1   51
2   1   52
3   2   53
4   3   50
5   3   51
1   2   52
2   1   53
3   1   50
4   2   51
5   3   52
1   3   53
2   2   50
3   1   51
4   1   52
5   2   53
1   3   50
2   3   51
3   2   52
4   1   53
5   1   50
1   2   51
2   3   52
3   3   53
4   2   50
5   1   51
1   1   52
2   2   53
3   3   50
4   3   51
5   2   52
1   1   53
2   1   50
3   2   51
4   3   52
5   3   53
1   2   50
2   1   51
3   1   52
4   2   53
5   3   183

内容的提问来源于stack exchange,提问作者T Simmons

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 01:40:00