Matplotlib绘制3D曲面图时仅加载前50组数据的问题求助
Matplotlib绘制3D曲面图时仅加载前50组数据的问题求助
我遇到了一个非常棘手的问题:使用下面的脚本绘制3D曲面图时,无论我的数据集有多少行,都只能显示前49行(也就是前50组(x,y,z)数据)。我特意在后面的行里设置了很高的z轴值,但这些值完全不会出现在生成的图里,只有前49行的数据能被渲染出来。
我已经花了整整一天时间尝试调整rstride/cstride或者rcount/ccount参数,但都没能解决这个问题。我甚至做了测试:把步长设为20,然后用25组数据,结果所有数据都能正常显示——这说明这不是降采样的问题,而是数据被截断了。
我确认过DataFrame里确实包含了全部200行数据,但3D曲面图就是只能处理前50组数据的样子。我找遍了相关模块的默认设置,却找不到哪里可以修改这个限制。肯定有很多人用Matplotlib绘制过超过50组数据的3D曲面图,我现在只需要能绘制200组数据的能力,实在不知道该怎么调整了,希望能得到大家的指导!
下面是我使用的代码:
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()
补充说明:我试过各种避免降采样的设置,但都没用,这根本不是分辨率的问题,就是数据被排除了——第49行之后的数据完全不被绘制。我怀疑是不是某个模块有默认限制,但找不到调整的地方。我也试过设置rcount和ccount为1,同样没有效果。DataFrame是完整的200行数据,但3D曲面图看起来就像是只能处理49x49的网格一样。
备注:内容来源于stack exchange,提问作者T Simmons
相关产品推荐
相关产品推荐

