PGFPLOTS 中的条件透明表面

PGFPLOTS 中的条件透明表面

我正在使用 PGFPLOTS 绘制 3D 条形图,按照示例并使用 Anton 在回答该问题(第二个)时给出的脚本:pgfplots 中的三维直方图

我的代码如下:

\documentclass{minimal}

\usepackage{pgfplots}

\pgfplotsset{compat=1.9}

\begin{document}

\begin{tikzpicture}[scale=0.7]
    \begin{axis}[
    view = {120}{25},% important to draw x,y in increasing order
    xmin = 0,
    ymin = 0,
    xmax = 9,
    ymax = 7,
    zmin = -0.20,
    zmax = 0.20,
    unbounded coords = jump,
    point meta min=-0.2, point meta max=0.2,
    colormap={pos}{color(0cm)=(blue); color(1cm)=(white); color(2cm)=(red)},
    xtick={1.5,3.5,5.5,7.5}, xticklabels={N,P,As,Sb},
    ytick={1.5,3.5,5.5}, yticklabels={Al,Ga,In},
    ztick={-0.2,-0.1,0,0.1,0.2}
    ]
    \addplot3[surf,mark=none,mesh/cols=16,faceted color=black] file {ratio};
    \end{axis}
\end{tikzpicture}

\end{document}

要运行它你需要文件名为ratio。输出如下所示:

在此处输入图片描述

我想让白色表面(值 z=0)透明,这样我就能看到表示负值的蓝色条。我可以使用该opacity=选项更改整个图的透明度,但我只希望白色水平表面透明。示意图,这将是 的任何正确等价物if (z == 0) then opacity = 0.5 ; else opacity = 1 ; fi。 可以实现这样的事情吗?

答案1

经过大量工作后,我对自己这个问题的回答很长。这有点像变通方法,而不是正确的答案(但效果很好),所以我不会接受它,并让别人真正回答如何使不透明度取决于 z 值的可能性。

首先,我编辑了 Anton 的 Python 代码pgfplots 中的三维直方图删除我不感兴趣的点:

import csv

def make3dhistogram(x, y, z, zmin, output):
    writer = csv.writer(open(output, 'wb'), delimiter=' ')
    for i in range(len(x)-1):        
#
# Pre-row for closing faces
        for j in range(len(y)-1):
            writer.writerow((x[i], y[j], "nan"))
            writer.writerow((x[i], y[j], zmin))
            writer.writerow((x[i], y[j], zmin))
            writer.writerow((x[i], y[j+1], zmin))
            writer.writerow((x[i], y[j+1], zmin))
            writer.writerow((x[i], y[j+1], "nan"))
        writer.writerow([])
#
# Background side of 3D bars
        for j in range(len(y)-1):
            writer.writerow((x[i], y[j], "nan"))
            writer.writerow((x[i], y[j], zmin))
            writer.writerow((x[i], y[j], z[i][j]))
            writer.writerow((x[i], y[j+1], z[i][j]))            
            writer.writerow((x[i], y[j+1], zmin))
            writer.writerow((x[i], y[j+1], "nan"))
        writer.writerow([])
#
# Foreground side of 3D bars
        for j in range(len(y)-1):
            writer.writerow((x[i+1], y[j], "nan"))
            writer.writerow((x[i+1], y[j], zmin))
            writer.writerow((x[i+1], y[j], z[i][j]))
            writer.writerow((x[i+1], y[j+1], z[i][j]))
            writer.writerow((x[i+1], y[j+1], zmin))
            writer.writerow((x[i+1], y[j+1], "nan"))          
        writer.writerow([])

x = [0,1,2,3,4,5,6,7,8,9]
y = [0,1,2,3,4,5,6,7]
z = [["nan", "nan", "nan", "nan", "nan", "nan", "nan"],
     ["nan", 0.155, "nan", 0.105, "nan", 0.155, "nan"],
     ["nan", "nan", "nan", "nan", "nan", "nan", "nan"],
     ["nan", 0.005, "nan", -0.055, "nan", 0.005, "nan"],
     ["nan", "nan", "nan", "nan", "nan", "nan", "nan"],
     ["nan", -0.025, "nan", -0.095, "nan", -0.055, "nan"],
     ["nan", "nan", "nan", "nan", "nan", "nan", "nan"],
     ["nan", -0.045, "nan", -0.115, "nan", -0.085, "nan"],
     ["nan", "nan", "nan", "nan", "nan", "nan", "nan"]]
make3dhistogram(x, y, z, 0.0, 'ratio')

上面的脚本现在用“nan”引用辅助点。运行时,它将创建一个名为的文件,ratio其中包含 latex 代码的坐标。现在,我想在 z = 0 处绘制平面并使其透明。为了实现这一点,我(按此顺序)绘制平面下方的条形图,然后绘制具有透明度的平面,然后绘制平面上方的条形图:

\documentclass[a4paper]{article}

\usepackage{amsmath}

\usepackage{pgfplots}
\usepackage[cm]{fullpage}
\pgfplotsset{compat=1.9}

\begin{document}
\thispagestyle{empty}

\begin{center}
\begin{tikzpicture}[scale=0.7]
    \begin{axis}[
    view = {120}{25},% important to draw x,y in increasing order
    xmin = 0,
    ymin = 0,
    xmax = 9,
    ymax = 7,
    zmin = -0.20,
    zmax = 0.20,
    unbounded coords = jump,
    point meta min=-0.2, point meta max=0.2,
    colormap={pos}{color(0cm)=(blue); color(1cm)=(white); color(2cm)=(red)},
    xtick={1.5,3.5,5.5,7.5}, xticklabels={N,P,As,Sb},
    ytick={1.5,3.5,5.5}, yticklabels={Al,Ga,In},
    ztick={-0.2,-0.1,0,0.1,0.2}
    ]
% Bars below z = 0
    \addplot3[surf,mark=none,mesh/cols=42,faceted color=black,
    restrict z to domain=-0.2:0] file {ratio};
% Plane with transparency
    \addplot3[surf,domain=0:9,samples=18,domain y=0:7,
    samples y=14,opacity=0.5] {0};
% Bars above z = 0
    \addplot3[surf,mark=none,mesh/cols=42,faceted color=black,
    restrict z to domain=0:0.2] file {ratio};
    \end{axis}
\end{tikzpicture}

Ratio $\zeta^\text{ele} / \zeta - \frac{5}{8}$
\end{center}

\end{document}

美丽的结果:

在此处输入图片描述

相关内容