x 轴坐标未出现

x 轴坐标未出现

我有一个如下所示的 tikzpicture:

\begin{center}
\begin{figure*}[tp]
 \centering
  \begin{subfigure}[b]{0.5\textwidth}
  \begin{tikzpicture}[scale=0.7]
  \begin{axis}[
        ylabel={Speedup},
        xlabel = {Cores},
        bar width=7mm, y=4mm,
        symbolic x coords={8,16,32,64,128,256,512},
        xtick=data,
        %ytick={0,500,1000},
        %log basis y=10,
        x tick label style={},
        nodes near coords align={vertical},
         legend cell align=left,
            legend style={
                    at={(1,1.05)},
                    anchor=south east,
                    column sep=1ex
            }
    ]

    \addplot [mark=diamond] coordinates{ %small
        (8,4.5) (16,6.2) (32,8.4) (64,9.6) 
    };
    \addplot [mark=o, color=blue] coordinates { %medium
       (64,16.8) (128,15.6) (256,16.8) (512,16.5)
    };
    \legend{Small, Medium}
\end{axis}
\end{tikzpicture}% pic 2
  \caption{\label{fig:speedup:small}}
\end{subfigure}
\end{figure*}
\end{center}

输出结果如图所示。

在此处输入图片描述

看起来 x 坐标 128,256,512 没有出现在图中。有人知道我做错了什么吗?

提前致谢。

答案1

xtick=data\addplot只从轴上的第一个位置获取数据。在这种情况下,只需删除xtick=data即可解决问题,我认为更通用的解决方案是将其替换为xtick distance=1

代码输出

\documentclass{article}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{figure}
\begin{subfigure}{0.5\textwidth}
  \centering
  \begin{tikzpicture}
  \begin{axis}[
        small, % added
        width=\linewidth, % added
        ylabel={Speedup},
        xlabel = {Cores},
        symbolic x coords={8,16,32,64,128,256,512},
        % not needed, but will force ticks at all data points
        % as symbolic coords have a unit distance of 1
        xtick distance=1, 
        legend cell align=left,
        legend style={
                    at={(1,1.05)},
                    anchor=south east,
                    column sep=1ex,
                    font=\small
            }
    ]

    \addplot [mark=diamond] coordinates{ %small
        (8,4.5) (16,6.2) (32,8.4) (64,9.6) 
    };
    \addplot [mark=o, color=blue] coordinates { %medium
       (64,16.8) (128,15.6) (256,16.8) (512,16.5)
    };
    \legend{Small, Medium}
\end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}
\end{document}

相关内容