繪製具有反向 X 軸的 X,Y 圖表

繪製具有反向 X 軸的 X,Y 圖表

我有以下數據集:

X= 130, 65, 45, 22, 14, 10
Y= 1.8, 1.5, 1.2, 1.1, 0.9, 0.8

首先,我寫的程式碼看起來很奇怪:

\begin{tikzpicture}
    \begin{axis}[
        title={My Chart},
        xlabel={X axis},
        ylabel={Y axis},
        ymin=0, ymax=2,
        xmin=0, xmax=140,
        ytick={1.8,1.5,1.2,1.1,0.9,0.8},
        xtick={10,14,22,45,65,130},
        legend pos=north west,
        ymajorgrids=true,
        grid style=dashed,
    ]

        \addplot[
            color=blue,
            mark=square,
        ]
        coordinates {
            (0,0)(1,1)(2,4)(3,9)(4,16)(5,25)(6,36)(7,49)(8,64)(9,81)(10,100)
        };
        \legend{Data 1}

    \end{axis}
\end{tikzpicture}

它將被渲染成這樣:

在此輸入影像描述

我想顯示下降趨勢,所以我想 X 軸必須反轉,但我不知道如何反轉。這是我理想中嘗試實現的圖表:

在此輸入影像描述

答案1

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[x dir=reverse]
\addplot+ coordinates {(0,0)(1,1)(2,4)(3,9)(4,16)(5,25)(6,36)(7,49)(8,64)(9,81)(10,100)};
\end{axis}
\end{tikzpicture}
\end{document}

圖形向下傾斜

答案2

因為你沒有提供你使用的文檔類別。我用standalone作為例子。這看起來很奇怪,有點想知道實際的應用。然而,這就是做到這一點的方法。

實際上有一種更簡單的方法,使用選項x dir=reverse

\documentclass[border=0.618cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    title={My Chart},
    xlabel={X axis},
    ylabel={Y axis},
    ymin=0.5, ymax=2,
    xmin=0, xmax=140,
    legend pos=north east,
    ymajorgrids=true,
    grid style=dashed,
    x dir=reverse
]
\addplot[
    color=blue,
    mark=square,
] coordinates {
    (130,1.8)(65,1.5)(45,1.2)(22,1.1)(14,0.9)(10,0.8)
};
\legend{Data 1}
\end{axis}
\end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容