python - 如何使用不确定条或等价物创建 3D 曲面图?

标签 python python-3.x matplotlib plot errorbar

目标是创建一个 3D 表面图,其中包含不确定条或其他一些清晰的不确定性可视化,如下所示:

目前,我有以下情节:

这是使用以下代码生成的:

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()

最佳答案

我不确定是否有内置功能,但我玩得很开心: 我为每个数据点制作了两个误差条(一个用于上面的部分,一个用于下面的部分),并用不同的 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)

enter image description here

关于python - 如何使用不确定条或等价物创建 3D 曲面图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56020777/

相关文章:

python - 如何在 Pygame 中滚动我的背景图像?

python - 如何使第一个索引列为空?

python - 将 NLTK 的通用标记集与非英语语料库结合使用

python - 使用Python在PE文件中搜索字符串

python - 多重最大标注 matplotlib

python - 将 Pandas hub_table 子图绘制到 matplotlib 图中创建一个新图

python - 如何将日期数组传递给 pcolor 图?

python - 如何手动安装 cx_Oracle rpm?

python - 在 Tkinter 中集成 click-Terminal?

python - 从文件中提取数据,处理然后下一行