admin管理员组

文章数量:1430997

Current this is my code for XY axis:

import numpy as np
import matplotlib.pyplot as plt

def plot_with_tricontourf_customized(dfs, figures_anizer, figsize_setting, list_points_mean, list_points_type, x_col, y_col, color_col, titles, marker_type='o', apply_limits=False, x_limits=None, y_limits=None):
    rows, cols = figures_anizer
    fig, axes = plt.subplots(rows, cols, figsize=(figsize_setting[0], figsize_setting[1] * rows), constrained_layout=True)
    axes = axes.flatten()  # Flatten the axes array for easier indexing

    for i, (ax, df, title, points_mean, points_type) in enumerate(zip(axes, dfs, titles, list_points_mean, list_points_type)):
        x = df[x_col].to_numpy()
        y = df[y_col].to_numpy()
        group = df[color_col].to_numpy()

        # Use a color map directly
        cmap = plt.colormaps['bwr']
        
        scatter = ax.scatter(x, y, c=group, cmap=cmap, edgecolor='black', marker=marker_type, s=40, alpha=0.8)
        contour = ax.tricontourf(x, y, group, levels=np.arange(-0.5, df[color_col].max() + 1.5), zorder=0, cmap=cmap, alpha=0.3)
        
        # Plot fixed points with specific markers
        for point, type in zip(points_mean, points_type):
            ax.scatter([point[0]], [point[1]], color='black', marker=type, s=100)

        # Add dashed lines passing through fixed points
        for point in points_mean:
            ax.axvline(x=point[0], color='black', linestyle='--')
            ax.axhline(y=point[1], color='black', linestyle='--')

        ax.set_xlabel(x_col)
        ax.set_ylabel(y_col)
        ax.set_title(title)
        ax.grid(True)

        if apply_limits:
            if x_limits:
                ax.set_xlim(x_limits)
            if y_limits:
                ax.set_ylim(y_limits)

        legend1 = ax.legend(*scatter.legend_elements(), loc="upper right", title=color_col)
        ax.add_artist(legend1)

        # Add a text box for fixed points explanation
        textstr = "★ : y=1 mean\n♦ : y=0 mean"
        props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
        ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=12,
                verticalalignment='top', bbox=props)

    # Hide any unused axes if there are more subplots than dataframes
    for ax in axes[len(dfs):]:
        ax.axis('off')

    plt.colorbar(scatter, ax=axes[:len(dfs)], orientation='horizontal', fraction=0.02, pad=0.04, ticks=np.arange(df[color_col].min(), df[color_col].max() + 1))
    plt.show()

# Example usage
# Suppose you have 15 dataframes, their means, types, and titles ready in lists
# plot_with_tricontourf_customized(dfs, (4, 4), list_points_mean, list_points_type, 'x', 'y', 'group', titles)

A sample output:

Now I want to plot on 3D with XYZ axis and color by volume. Similar to my shown output, but now in 3D.

I am looking for:

  • Draw this on 3D, how should I adjust my code
  • Option to adjust the view of 3D

Thank you

本文标签: pandasPythonXYZ plot coloring cluster regionStack Overflow