Tikz 绘图问题

Tikz 绘图问题

我在 Tikz 上绘图时遇到了一些问题。我想生成这样的图,

在此处输入图片描述

但相反,我的 Tikz 生成了这个。

在此处输入图片描述

我怎样才能改进我的 Tikz?

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc} % International characters
\usepackage[T1]{fontenc} % Font encoding for international characters
% \usepackage{fouriernc} % Use the New Century Schoolbook font
\usepackage{lmodern} % Use Latin Modern font
% \usepackage{libertine} % Use LLibertine font
% \usepackage{libertinust1math}% Use LLibertine math font
\usepackage{geometry} % Setting page geometry (margins)
\usepackage{blindtext} % Lorem ipsum
\usepackage{textcomp} % For the trademark symbol
\usepackage{booktabs} % Allows the use of \toprule, \midrule and \bottomrule in tables for horizontal lines
\usepackage{siunitx} % Allows for properly formatted numbers with \num{number}, units with \si{unit} and both with \si{number}{unit}
\usepackage{microtype} % A few refinements to typography that are hardly noticeable
\usepackage[normalem]{ulem} %Strikethrough text
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{calc}

%Tables
\usepackage{float} % image positioning
\usepackage{multirow}
\usepackage{array}
\usepackage{tabu}
\usepackage{tikz}
\usepackage{longtable}
\usepackage{caption}
\usepackage{tabularx}

%Itemize
\usepackage{etoolbox,refcount}
\usepackage{multicol}
 \usepackage{setspace}

%Fonts
\usepackage{titlesec}
\titleformat{\section}{\normalfont\fontsize{11pt}{11pt}\selectfont\bfseries}{\thesection}{1em}{}
\titleformat*{\subsection}{\normalsize\bfseries}

%Chemistry
\usepackage[version=3]{mhchem}
\usepackage[separate-uncertainty]{siunitx}
\DeclareSIUnit\molar{\mole\per\cubic\deci\metre}
\DeclareSIUnit\Molar{\textsc{M}}

%Page Number
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear all header and footer fields
\fancyfoot[R]{\thepage} % page number on right bottom

%Graphs
\usepackage{pgfplots} %Graph
\pgfplotsset{compat=1.5}
\usepackage{graphicx}
\usepackage{adjustbox}

\usepackage{amsmath} % amsmath も必要
\usepackage{mathtools}
\mathtoolsset{showonlyrefs}

% Package must come last
\usepackage[bookmarks=true]{hyperref} % Hyperrefs for bookmarks
\hypersetup{
    colorlinks=true,
    citecolor=black,
    linkcolor=blue,
    filecolor=magenta,      
    urlcolor=blue,
}
\usepackage{bookmark} % Adds metadata to PDF for bookmarking

% Package settings & macros

\renewcommand{\contentsname}{Contents} % Contents heading

\urldef{\usePercentage}\url{%} % Using the percent symbol in documents: \usePercentage

\usepackage[citestyle=authoryear,bibstyle=mla,sorting=nty]{biblatex} % bibliography
\usepackage{url}
\usepackage{appendix}

\renewcommand{\nameyeardelim}{\addcomma\addspace}
\addbibresource{references.bib}
\urlstyle{same}
\renewcommand*{\bibfont}{\scriptsize}

% Document Settings
\geometry{margin=1in}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\renewcommand{\headrulewidth}{0pt} 

\newlength{\conditionwd}
\newenvironment{conditions}[1][where:]
  {%
   #1\tabularx{\textwidth-\widthof{#1}}[t]{
     >{$}l<{$} @{${}={}$} X@{}
   }%
  }
  {\endtabularx\\[\belowdisplayskip]}
\newcounter{countitems}
\newcounter{nextitemizecount}
\newcommand{\setupcountitems}{%
  \stepcounter{nextitemizecount}%
  \setcounter{countitems}{0}%
  \preto\item{\stepcounter{countitems}}%
}
\makeatletter
\newcommand{\computecountitems}{%
  \edef\@currentlabel{\number\c@countitems}%
  \label{countitems@\number\numexpr\value{nextitemizecount}-1\relax}%
}
\newcommand{\nextitemizecount}{%
  \getrefnumber{countitems@\number\c@nextitemizecount}%
}
\newcommand{\previtemizecount}{%
  \getrefnumber{countitems@\number\numexpr\value{nextitemizecount}-1\relax}%
}
\makeatother    
\newenvironment{AutoMultiColItemize}{%
\ifnumcomp{\nextitemizecount}{>}{3}{\begin{multicols}{2}}{}%
\setupcountitems\begin{itemize}}%
{\end{itemize}%
\unskip\computecountitems\ifnumcomp{\previtemizecount}{>}{3}{\end{multicols}}{}}

\begin{document}

\begin{figure}[H]
\caption{the effect of increasing \ce{CuSO4} concentrations on voltage produced}\label{fg:plot}
\begin{center}
\begin{adjustbox}{max width = 200mm}
\begin{tikzpicture}
\pgfplotsset{width=5in, height=4in}
\begin{axis}[
axis lines = left,
xlabel = concentration of \ce{CuSO4} (\si{mol.dm^{-3}}),
ylabel = mean voltage produced (\si{V}),
xmin=-3, xmax=8,
ymin=1.550, ymax=1.950,
xtick={-3.0, -2.0, -1.0, 0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8},
ytick={1.550, 1.600, 1.650, 1.700, 1.750, 1.800, 1.850, 1.900},
legend pos=north west,
]
\addplot [
domain=-2.302585093:6.907755279,
samples=1000,
color=blue,
]
{-0.0301*x+1.7967};
\addlegendentry {$-0.0301x+1.7967$}
\addplot[
only marks,
color=blue,
mark=*,
error bars/.cd,
y dir=both, y explicit,
x dir=both, x explicit,
]
coordinates {
(6.907755279, 1.586) +- (0, 0)
(4.605170186, 1.662) +- (0, 0)
(2.302585093, 1.732) +- (0, 0)
(0, 1.789) +- (0, 0)
(-2.302585093, 1.869) +- (0, 0)
};
\end{axis}
\end{tikzpicture}
\end{adjustbox}
\end{center}
\end{figure}

\end{document}

答案1

这是一种实现此目的的方法,至少在大多数情况下是这样。

首先:如果您想要看起来像Excel,请使用 Excel,例如将您的图表保存为位图并将其包含在内,例如通过包graphicx

我将您的代码精简为此处所需的基本代码:所有其他代码可能与您的文档相关,但在此处无关。(因此下次请在发布之前像我一样通过丢弃来归纳问题。)

据我所知,pgfplot旨在创建用于科学出版物和类似出版物的图纸。因此,出于这个原因,根本不提供移动 y 轴的功能。(如果可以,它可能会掩盖轴上的值。)但是,您会在本网站和手册的其他地方找到一种常见的解决方案,即绘制一些extra ticks,假装是一些额外的轴。

除了删除之外,我还对您的代码做了一些事情:

  • 使用\documentclass[10pt,border=3mm,tikz]{standalone}使创建和关注 tikz 图表更加容易
  • 为了遵循示例中的 x 轴,我将 xtick 减小到xtick={-2.0, 0, 2.0, 4.0, 6.0, 8},
  • 我忽略了\ce{}宏:这样你就能更轻松地找出需要哪些代码才能使该宏工作

我还把这些陈述放在绘制两个主要网格中,并为 y 轴放置一些次要网格,方法是采用以下解决方案 手动指定刻度时,pgfplot 中的小网格不显示

        grid=major,
        yminorgrids,
        minor y tick num =5,
        minor grid style={draw=gray!20,line width=1pt},% adjust as needed
        extra x ticks={0},% page 99
        extra x tick style={grid style={black}},% adjust as needed

顺便说一句,你可以,但你不需要使用坐标。只需在手册中查找scatter plot和。table data

结果

\documentclass[10pt,border=3mm,tikz]{standalone}
\usepackage{siunitx} % Allows for properly formatted numbers with 
%%Chemistry
%\usepackage[version=3]{mhchem}
%\usepackage[separate-uncertainty]{siunitx}
%\DeclareSIUnit\molar{\mole\per\cubic\deci\metre}
%\DeclareSIUnit\Molar{\textsc{M}}

%Graphs
\usepackage{pgfplots} %Graph
\pgfplotsset{compat=1.5}

\begin{document}

 \begin{tikzpicture}
  \pgfplotsset{width=5in, height=4in}
  \begin{axis}[
        axis lines = left,
        xlabel = concentration of CuSO4 (\si{mol.dm^{-3}}),
        ylabel = mean voltage produced (\si{V}),
        xmin=-3, xmax=8,
        ymin=1.550, ymax=1.950,
%       xtick={-3.0, -2.0, -1.0, 0, 1.0, 2.0, 3.0, 4.0, 5.0, 
%               6.0, 7.0, 8},
        xtick={-2.0,  0, 2.0, 4.0, 6.0,  8},
        ytick={1.550, 1.600, 1.650, 1.700, 1.750, 1.800, 
               1.850, 1.900},
        legend pos=north west,
        grid=major,
        yminorgrids,
        minor y tick num =5,
        minor grid style={draw=gray!20,line width=1pt},
        extra x ticks={0},% page 99
        extra x tick style={grid style={black}},
    ]
    \addplot [
        domain=-2.302585093:6.907755279,
        samples=1000,
        color=blue,
    ]
    {-0.0301*x+1.7967};
    
    \addlegendentry {$-0.0301x+1.7967$}
    
    \addplot[
        only marks,
        color=blue,
        mark=*,
        error bars/.cd,
        y dir=both, y explicit,
        x dir=both, x explicit,
    ]
    coordinates {
        (6.907755279, 1.586)    +- (0, 0)
        (4.605170186, 1.662)    +- (0, 0)
        (2.302585093, 1.732)    +- (0, 0)
        (0, 1.789)              +- (0, 0)
        (-2.302585093, 1.869)   +- (0, 0)
    };
  \end{axis}
 \end{tikzpicture}

\end{document}

答案2

  • 请在下次提供 MWE(最小工作示例),其中序言仅加载到与问题相关的包中,并且仅包含与问题相关的定义(请参阅在收到的答案中如何构建 MWE)。
  • 有些软件包已经过时了。我强烈建议您将 LaTeX 安装升级到最新版本(pgfplots1.18、mchem版本 4 等)。
  • 我的答案是 MS-SPO 答案的小变化(不同之处在于使用的图表轴、删除的刻度位置等):
  • MWE 重现了您问题中显示的第一张图片:
\documentclass[border=3mm]{standalone}
\usepackage{siunitx}
\usepackage[version=4]{mhchem}  % <---

%Graphs
\usepackage{pgfplots} %Graph
\pgfplotsset{compat=1.18}       % <---

\begin{document}
 \begin{tikzpicture}
  \pgfplotsset{width=5in, height=4in}
  \begin{axis}[
        axis lines = middle,
        title= Chart title,
        xlabel = concentration of \ce{CuSO4} (\si{mol.dm^{-3}}),
        xlabel style = {anchor=north east, yshift=-3ex},
        ylabel = mean voltage produced (\si{V}),
        ylabel style = {xshift=-3cm, rotate=90, anchor=south east},
        xmin=-3,    xmax=8,
        ymin=1.55,  ymax=1.9,
        legend pos=north east,
        ymajorgrids,
        yminorgrids,
        minor y tick num =4,
        minor grid style={draw=gray!20,very thin},
        xtick distance = 2,
        tick label style = {font=\footnotesize, fill=white},
        extra x ticks={0},% page 99
        extra x tick style={grid style={black}},
    ]
\addplot [domain=-2.4:7,samples=2,color=blue]{-0.0301*x+1.7967};
    \addlegendentry {$-0.0301x+1.7967$}
\addplot[only marks, mark=*,color=blue,
        %error bars/.cd,    % not neede if "+- (0, 0.)"
        %y dir=both, y explicit,
        %x dir=both, x explicit
         ]    coordinates {
        (6.907755279, 1.586)    %+- (0, 0.01)
        (4.605170186, 1.662)    %+- (0, 0.01)
        (2.302585093, 1.732)    %+- (0, 0.01)
        (0, 1.789)              %+- (0, 0.01)
        (-2.302585093, 1.869)   %+- (0, 0.01)
        };
  \end{axis}
 \end{tikzpicture}
\end{document}

在此处输入图片描述

编辑: 更正了拼写错误的 MS-SPO 名称。抱歉,但他确实是专业人士!

相关内容