如何繪製兩個xticks之間的座標?

如何繪製兩個xticks之間的座標?

我想使用 pgfplot 繪製一些數據。這是我寫的程式碼:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  legend style={legend pos = north west, font=\footnotesize, draw=none},
  legend cell align=left,
  xtick=data,
  ytick={0,5,10,...,45},
  xticklabels={0,2,4,...,16,18},
  every axis label={font=\footnotesize},
  tick label style={font=\footnotesize},
  label style={font=\footnotesize},
  enlarge x limits=false,
  enlarge y limits=false]
  \addplot[lightgray, mark=*] table [x expr = \lineno, y = Time] {Data.dat};
  \addlegendentry{x}
  \end{axis}
\end{tikzpicture}
\end{document}

Data.dat文件內容如下:

 Points Time
   0        0
   2000     1.11
   4000     3.54
   6000     7.35
   8000     12.43
   10000    18.15
   12000    24.51
   14000    31.52
   16000    38.59
   17791    45.35

由於我是該網站的新手,因此無法發布輸出圖。在輸出圖中,最後一個點 (17791, 45.35) 錯誤地繪製在 x=18 和 y=45.35 處。
所以我的問題是:如何在點 x=16 和 x=18 之間正確繪製最後一點?

答案1

該點精確繪製在x=17.791, y=45.35;問題是最後一個刻度使用的標籤是錯誤的(您手動將其聲明為 18,這會覆蓋預設值17.8);問題不在於繪圖:問題在於放置 x 標籤(特別是最後一個標籤)的方式。

在下面的範例中,我展示了一種糾正這種情況的可能方法:我讓xtick=data(現在自動從表中選取刻度線及其標籤)並使用extra x ticks輔助extra xtick labels藍色網格來顯示與18 相對應的點的實際位置;我還更改了使用的標記(在我看來,您使用的標記太大,可能會產生位於 的印象x=18),並且還使用了不同的顏色以實現可視化目的。如您所看到的,最後一個點的 x 座標正是它應該在的位置(在為 繪製的紅色垂直線左側一點x=18):

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.3}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[
  xmax=18500,
  legend style={legend pos = north west, font=\footnotesize, draw=none},
  legend cell align=left,
  xtick=data,
   ytick={0,5,10,...,45},
   extra x ticks={18000},
   extra x tick labels={},
   extra x tick style={grid=major,tick label style={rotate=90,anchor=east}},
   major grid style={color=red},
   every axis label={font=\footnotesize},
   tick label style={font=\footnotesize},
  label style={font=\footnotesize},
  enlarge x limits=false,
  enlarge y limits=false,
  scaled x ticks=base 10:-3
]
  \addplot[blue, mark=x] table  {Data.dat};
  \addlegendentry{x}
  \end{axis}
\end{tikzpicture}
\end{document}

在此輸入影像描述

您可以使用明確列表xmark

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.3}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[
  xmax=18500,
  ymax=46,
  legend style={legend pos = north west, font=\footnotesize, draw=none},
  legend cell align=left,
   xtick={0,2000,4000,6000,8000,...,14000,16000,18000},
   ytick={0,5,10,...,45},
  major grid style={color=red},
  every axis label={font=\footnotesize},
  tick label style={font=\footnotesize},
  label style={font=\footnotesize},
  enlarge x limits=false,
  enlarge y limits=false,
  scaled x ticks=base 10:-3
]
  \addplot[blue, mark=x] table  {Data.dat};
  \addlegendentry{x}
  \end{axis}
\end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容