The goal is to create a 3D surface plot featuring uncertainty bars or some other clear visualisation of uncertainties, like this:
Currently, I have the following plot:
This generated using the following code:
from io import StringIO
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
import seaborn as sns
data_string = StringIO(
"""
V1, V2, V3, V4, V5
110,111,109,107,108
101,101,102,102,102
102,102,103,103,103
103,103,104,104,104
100,101,100,100,100
"""
)
uncertainties_string = StringIO(
"""
V1U,V2U,V3U,V4U,V5U
5, 5, 5, 5, 7
5, 5, 3, 5, 5
6, 5, 5, 5, 5
5, 6, 5, 2, 5
5, 5, 5, 5, 5
"""
)
data = pd.read_csv(data_string)
uncertainties = pd.read_csv(uncertainties_string)
df = data.unstack().reset_index()
df.columns = ["X", "Y", "Z"]
df['X'] = pd.Categorical(df['X'])
df['X'] = df['X'].cat.codes
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_trisurf(df['Y'], df['X'], df['Z'], cmap=plt.cm.jet, linewidth=0.01)
fig.colorbar(surf)
plt.show()
I am not sure if there is built-in functionality for this, but I had fun playing around:
I make two error bars per data point (one for the part above and one for the part below), and plot them with different zorder
:
# get errors into the same format as data
df_unc = uncertainties.unstack().reset_index()
df_unc.columns = ["X", "Y", "Z"]
df_unc['X'] = pd.Categorical(df_unc['X'])
df_unc['X'] = df_unc['X'].cat.codes
# compute lower and upper values for errorbars
df['z_low'] = df['Z'] - df_unc['Z']
df['z_high'] = df['Z'] + df_unc['Z']
# plot surface with middle value for zorder
surf = ax.plot_trisurf(df['Y'], df['X'], df['Z'], cmap=plt.cm.jet, linewidth=0.01, zorder=2)
# plot lines with lower and higher zorder, respectively
for ix, row in df.iterrows():
ax.plot((row['Y'], row['Y']), (row['X'], row['X']), (row['z_low'], row['Z']), c='k', zorder=1)
ax.plot((row['Y'], row['Y']), (row['X'], row['X']), (row['Z'], row['z_high']), c='k', zorder=3)