Skip to content

Instantly share code, notes, and snippets.

@kylecampbell
Created October 20, 2017 19:27
Show Gist options
  • Select an option

  • Save kylecampbell/86d5a757cd860b3c6602eeb02850d88f to your computer and use it in GitHub Desktop.

Select an option

Save kylecampbell/86d5a757cd860b3c6602eeb02850d88f to your computer and use it in GitHub Desktop.
3D Plots with Python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np
import pandas as pd
import seaborn as sns
#uncomment below if using Jupyter
#%config InlineBackend.figure_format = 'retina'
# get data
df = pd.read_csv('../some/data/path')
# 3D Plot 1: all same color
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(df.col_a, df.col_b, df.col_c, zdir='z', s=20, c=None)
plt.show()
# 3D Plot 2: three different colors based on a property value
df_aug = pd.merge(df, some_prob, left_on='some_col_name', right_index=True)
df_aug['some_property'] = df_aug['some_col'].apply(lambda x: 0 if x=='no' else 2 if x=='yes' else 1)
nx = df_aug.col_a[df_aug.some_property==0]
ny = df_aug.col_b[df_aug.some_property==0]
nz = df_aug.col_c[df_aug.some_property==0]
yx = df_aug.col_a[df_aug.some_property==2]
yy = df_aug.col_b[df_aug.some_property==2]
yz = df_aug.col_c[df_aug.some_property==2]
ox = df_aug.col_a[df_aug.some_property==1]
oy = df_aug.col_b[df_aug.some_property==1]
oz = df_aug.col_c[df_aug.some_property==1]
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(111, projection='3d')
ax1 = ax.scatter(nx, ny, nz, zdir='z', s=20, c='b')
ax2 = ax.scatter(yx, yy, yz, zdir='z', s=20, c='r')
ax3 = ax.scatter(ox, oy, oz, zdir='z', s=20, c='y')
ax.set_title('Answers - col_a vs col_b vs col_c')
plt.show()
# 3D Plot 3: stacked with colorbar
allx = df_aug.col_a
ally = df_aug.col_b
allz = df_aug.some_property
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(111, projection='3d')
pnt3d = ax.scatter(allx, ally, allz, s=20, c=allz, cmap='coolwarm')
cbar = plt.colorbar(pnt3d)
cbar.set_label("No - Other - Yes")
ax.set_title('All Answers - col_a vs col_b')
plt.show()
@masimejo-collab

Copy link
Copy Markdown

Please download for me the diagrams: import plotly.graph_objects as go
import numpy as np

Sample data

time = np.array([50, 100, 150, 200, 250])
temp = np.array([30, 40, 50, 60, 70])
yield_ = np.array([65, 70, 80, 85, 90])

Scatter only

scatter = go.Scatter3d(
x=time, y=temp, z=yield_,
mode='markers',
marker=dict(size=6, color=yield_, colorscale='Viridis', showscale=True),
name="Experimental Data"
)

fig1 = go.Figure(data=[scatter])
fig1.update_layout(scene=dict(
xaxis_title="Time (min)",
yaxis_title="Temperature (°C)",
zaxis_title="Yield (%)"
))
fig1.write_html("scatter_only.html")

Scatter + surface

time_grid = np.linspace(time.min(), time.max(), 20)
temp_grid = np.linspace(temp.min(), temp.max(), 20)
T, Temp = np.meshgrid(time_grid, temp_grid)
Z = 60 + 0.1T + 0.3Temp # simple plane fit

surface = go.Surface(x=time_grid, y=temp_grid, z=Z, colorscale="Jet", opacity=0.6)
fig2 = go.Figure(data=[scatter, surface])
fig2.update_layout(scene=dict(
xaxis_title="Time (min)",
yaxis_title="Temperature (°C)",
zaxis_title="Yield (%)"
))
fig2.write_html("scatter_surface.html")

Scatter + surface + contours

surface_contour = go.Surface(
x=time_grid, y=temp_grid, z=Z,
colorscale="Jet", opacity=0.6,
contours=dict(
x=dict(show=True, highlight=True),
y=dict(show=True, highlight=True),
z=dict(show=True, highlight=True)
)
)
fig3 = go.Figure(data=[scatter, surface_contour])
fig3.update_layout(scene=dict(
xaxis_title="Time (min)",
yaxis_title="Temperature (°C)",
zaxis_title="Yield (%)"
))
fig3.write_html("scatter_surface_contours.html")

print("✅ Files saved: scatter_only.html, scatter_surface.html, scatter_surface_contours.html")

@masimejo-collab

Copy link
Copy Markdown

How do i get the saved files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment