admin管理员组

文章数量:1126309

Having looked at the documentation and having tried many implementations of rstride and rcount to force mplot3d to plot more than 50 x,y,z datasets. I am not getting downsampling, I am getting truncation. The 3d surface that is plotted only consists of the first 49 xyz points. I cannot find a way to exceed that number to plot a surface of 200 xyz points.

I have tried plotly, but it is just a wrapper for the same issue, although a nice one. I have tried setting rstride/rcount both high (200) and low (1), tried to play with the meshgrid, lots of little things that I was sure would fix...but in the end, it always just plots the first 49 points. I can get 3d scatters of all my points within other modules of matplotlib, however, i can not get a surface or tri-surface.

I am aware that the default limit is set to 50, and that beyond this it will downsample, but I cannot square this with the fact that it is simply discarding any and all data beyond 49th xyz set.

I have inspected the df and all the data is there, up to 200 rows, but the plot always truncates after 49th set.

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

本文标签: