Skip to content Skip to sidebar Skip to footer

How Can Get Scatter 3d-plot Using Different Dataframes To Set Ax.scatter Parameters?

Recently, I'm suffering to demonstrate a 3D-scatter plot using 2 different dataframes. The idea is to offer a 3D-scatter plot including 2 legends for reporting the results of the c

Solution 1:

I tried to reproduce only the graphing part with the updated Colab. I have traced your code and noticed something. I think the error in running the function is caused by the number of colors not matching the number of data. The graph is created as 20 data by cutting only the important data from your code.

color_names = ["red", "blue", "yellow", "black", "pink", "purple", "orange"]

def plot_3d_transformed_data(df, title, colors="red"):

  # Imports.
  import matplotlib as mpl
  import matplotlib.pyplot as plt
  from mpl_toolkits.mplot3d import Axes3D
  import pandas as pd
  import numpy as np
  import plotly.express as px
  import matplotlib.cm as cm

  #clusterSizes = pd.read_csv(io.StringIO(data1), delim_whitespace=True)

  #pddf_pred = df_pred.set_index('id')
  cluster_Sizes = clusterSizes["size"].unique()
  #x_train = np.random.randint(20,500,(20,))

  # Figure.
  figure = plt.figure(figsize=(12, 10))
  ax = figure.add_subplot(projection="3d")
  ax.set_xlabel("PC1: x")
  ax.set_ylabel("PC2: y")
  ax.set_zlabel("PC3: z")
  ax.set_title("scatter 3D legend") 

  colors2 = ["red", "blue", "yellow", "black", "pink", "purple", "orange", "black", "red" ,"blue"]
  colors = cm.rainbow(np.linspace(0, 1, 20))

  # Create 3D scatter plot
  sc = ax.scatter(pddf_pred.x.values, pddf_pred.y.values, pddf_pred.z.values, alpha=0.6, s=x_train, c=colors, marker="o")

  # Legend 1.
  handles, labels = sc.legend_elements(prop="sizes", alpha=0.6)
  legend1 = ax.legend(handles, labels, bbox_to_anchor=(1.2, 1), loc="upper right", title="Sizes")
  ax.add_artist(legend1)

  # Legend 2.
  unique_colors = set(colors2)
  handles = []
  labels = []
  for n, color in enumerate(unique_colors, start=1):
      artist = mpl.lines.Line2D([], [], color=color, lw=0, marker="o")
      handles.append(artist)
      labels.append(str(n))
  legend2 = ax.legend(handles, labels, bbox_to_anchor=(-0.05, 0.05), loc="lower left", title="Classes")

  plt.show()

Post a Comment for "How Can Get Scatter 3d-plot Using Different Dataframes To Set Ax.scatter Parameters?"