Created
October 20, 2017 19:27
-
-
Save kylecampbell/86d5a757cd860b3c6602eeb02850d88f to your computer and use it in GitHub Desktop.
3D Plots with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do i get the saved files?