在 pgfplots 中使用顏色圖繪製多條線

在 pgfplots 中使用顏色圖繪製多條線

我在這個帖子中向 Steve Hatcher 提出了類似的問題: 多條曲線的光譜顏色圖

我有一個包含多列的資料檔案(mwe_data.txt)。第一列是 x 軸,其餘所有列是 y1、y2、y3、...yn。

# mwe_data.txt:
# x y1  y2  y3  y4  y5  y6
0.0 -1.6    0.5 1.5 5.8 8.7 12
0.10    10.5    9.3000001907    10.1000003815   15.1999998093   19.7000007629   19.2000007629
0.20    17.7999992371   14.3000001907   13.3999996185   16.5    20.7000007629   20.2000007629
0.40    28.6000003815   26.2999992371   23.7000007629   23.2999992371   21  24
0.60    33.0999984741   29.3999996185   26.2999992371   25.3999996185   22  25
0.70    36.9000015259   32.2999992371   28.1000003815   25.6000003815   26.1000003815   27

我想根據 x 列繪製所有 y 列。我希望我的數據由點表示,並且我希望從光譜顏色圖中選擇特定列的顏色。我想使用 pgfplots。

下面是我的 python 腳本,它產生我想要的圖表:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cmplt

plt.ion()

mydata = np.loadtxt('mwe_data.txt', dtype=float)
mylegend = ["Jan 1", "Feb 1", "Mar 1", "Apr 1", "May 1", "Jun 1"]

plt.rc('text', usetex=True)

plt.figure()
plt.xlim(-0.05, 0.75)

maxcols = np.shape(mydata)[1]
cmdiv = float(maxcols)

for ii in range(1, maxcols):
    xaxis = mydata[:, 0]
    yaxis = mydata[:, ii]
    plt.plot(xaxis, yaxis, "o", label=mylegend[ii-1],
             c=cmplt.spectral(ii/cmdiv, 1))

plt.legend(loc='lower right', frameon=False)

結果如下:Python:完美的圖表

在 LaTeX 中,我到目前為止已經做到了:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{
  compat = 1.9,
  every axis legend/.append style={draw=none, font=\footnotesize, legend cell align = left, at={(0.95, 0.05)}, anchor=south east}}

\begin{document} 
\begin{tikzpicture}
\begin{axis}[
colormap/jet]
\def\maxcols{6}
\foreach \i in {1, 2, ..., \maxcols}
\addplot+[mark=*, only marks] table[x index=0, y index=\i] {mwe_data.txt};
\legend{Jan 1, Feb 1, Mar 1, Apr 1, May 1, Jun 1}
\end{axis}
\end{tikzpicture}

\end{document}

結果是:圖,乳膠 1,第一次嘗試

在這裡我不知道如何將線/點的顏色變更為光譜。我嘗試使用我在開頭提到的線程中建議的解決方案,並得到以下結果(這裡我必須修改數據以獲取與光譜中的顏色相對應的 z 坐標):

# mwe_data_3d.txt:
# x y   z (colour)
0.0 -1.6    0
0.10    10.6    0
0.20    17.7999992371   0
0.40    28.6000003815   0
0.60    33.0999984741   0
0.70    36.9000015259   0

0.0 0.6 0.2
0.10    9.3 0.2
[...]

0.0 12  1
0.10    19.2    1
0.20    20.2    1
0.40    24  1
0.60    25  1
0.70    27  1

以及 LaTeX 代碼:

\documentclas{standalone}
\usepackage{pgfplots}
\pgfplotsset{
  compat = 1.9,
  every axis legend/.append style={draw=none, font=\footnotesize, legend cell align = left, at={(0.95, 0.05)}, anchor=south east},}

\begin{document} 

\begin{tikzpicture}
    \begin{axis}[
        view={0}{90},
        colormap/jet,
    ]
    \addplot3[
        only marks,
        mark=*,
        mesh,
        patch type=line,
        point meta=z,
    ]
    table {mwe_data_3d.txt};
    \legend{Jan 1, Feb 1, Mar 1, Apr 1, May 1, Jun 1}

    \end{axis}
\end{tikzpicture}
\end{document}

結果是: 圖,latex 2,第二次嘗試

最後一個解決方案給了我我想要的顏色,但還有其他問題:

  1. 傳說顯然是錯誤的
  2. 標記都是黑色的,而我希望它們的顏色與線條相同
  3. 我希望能夠使用“僅標記”選項。

有什麼建議麼?

答案1

您可以使用以下方法使用自訂顏色圖繪製一個 x 與多個 y

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\begin{filecontents}{mwe_data.txt}
x y1  y2  y3  y4  y5  y6
0.0 -1.6    0.5 1.5 5.8 8.7 12
0.10    10.5    9.3000001907    10.1000003815   15.1999998093   19.7000007629   19.2000007629
0.20    17.7999992371   14.3000001907   13.3999996185   16.5    20.7000007629   20.2000007629
0.40    28.6000003815   26.2999992371   23.7000007629   23.2999992371   21  24
0.60    33.0999984741   29.3999996185   26.2999992371   25.3999996185   22  25
0.70    36.9000015259   32.2999992371   28.1000003815   25.6000003815   26.1000003815   27
\end{filecontents}


\begin{document}

\begin{tikzpicture}
\begin{axis}[colormap/jet]
\foreach \i in {1,...,6}{
\addplot [scatter, only marks, point meta=\i] table [y index=\i] {mwe_data.txt};
}
\end{axis}
\end{tikzpicture}
\end{document}

相關內容