我在使用 pgfplots barplot 时遇到了一点小问题。我试图构建的东西相对简单,而且我一辈子也看不出我的错误在哪里。有趣的是,当我尝试使用 pgfplot 代码进行编译时,我得到了各种引用错误.....注释掉 pgfplots 代码并再次编译,一切正常,没有错误。
pgfplots 代码:
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[
ybar,
width=.8\linewidth,
height=7cm,
title=Flutningstakmarkanir yfir spátímabilið,
xlabel={Sviðsmyndir},
ylabel={Milljón \$ },
symbolic x coords={Mynd 1,Mynd 2, Mynd 3},
xtick=data,
legend columns=-1, % to set one line legend
legend style={at={(0.5,-0.3)},anchor=center},
ymajorgrids
]
\addplot+[ybar] coordinates { (mynd1,98) (mynd2,59) (mynd3,43)};
\addplot+[ybar] coordinates { (mynd1,95) (mynd2,57) (mynd3,42)};
\end{axis}
\end{tikzpicture}
\end{figure}
我正在使用的所有软件包:
\documentclass[a4paper,12pt,twoside,BCOR=10mm]{scrbook}
% Packages
\usepackage[utf8]{inputenc}
\usepackage[icelandic]{babel}
\usepackage{t1enc}
\usepackage{graphicx,booktabs}
\usepackage[intoc]{nomencl}
\usepackage{enumerate,color}
\usepackage{url}
\usepackage[pdfborder={0 0 0}]{hyperref}
\BeforeTOCHead[toc]{\cleardoublepage\pdfbookmark{\contentsname}{toc}} % Add Table of Contents to PDF "bookmark" table of contents
\usepackage{appendix}
\usepackage{eso-pic}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{longtable}
\usepackage[sf,normalsize]{subfigure}
\usepackage[format=plain,labelformat=simple,labelsep=colon]{caption}
\usepackage{placeins}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{subfigure}
\usepackage{adjustbox}
% Packages used for title page layout
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{external}
% Blue color according to HÍ corporate design
\convertcolorspec{RGB}{16,9,159}{rgb}\tmphiblue
\definecolor{hiblue}{rgb}\tmphiblue
\setlength{\parskip}{\baselineskip}
\setlength{\parindent}{0cm}
\raggedbottom
\setkomafont{captionlabel}{\itshape}
\setkomafont{caption}{\itshape}
\setkomafont{section}{\FloatBarrier\Large}
\setcapwidth{\textwidth}
%\setcapwidth[l]{\textwidth} % The original template had the [l] which leads to a warning that it gets ignored, so to reduce warnings, removed it.
\setcapindent{1em}
\usepackage{lmodern} % Use Latin Modern (instead of the default Computer Modern that is rendered using a bitmap font).
\usepackage{fixcmex} % To fix that Latin Modern large symbol math fonts has by default only one size: https://tex.stackexchange.com/a/621536
% Times new roman font instead of the standard LaTeX fonts: has not been test -- try this on your own risk
\usepackage[T1]{fontenc}
\usepackage{mathptmx}
%%%%%%%%%%%%%%%%% Configurations (Useful defaults, but OK to change %%%%%%%%%%%%%%%%%%%
\graphicspath{{figs/}} % Figures in directory figs
% Bibliography
% \usepackage[authoryear]{natbib} % Uncoment if you want to used NatBib instead of BibLaTeX (and comment the bitlatex line below)
\usepackage[backend=biber, style=authoryear]{biblatex} % BibLaTeX used for references.
\usepackage{csquotes} % BibLaTex wants to have context sensitive quotes
\addbibresource{references.bib} % Name of *.bib file containing references
我确实以类似的方式设置了 pgfplots 线图,效果非常好。我查阅的大多数文章都以类似的方式设置了 pgfplots 代码。我只想将我的语言用于标签、标题和类似的东西。如果有人能指出我的错误,我将不胜感激 :)
答案1
- 请始终提供 MWE(最小工作示例),即一个可以重现您的问题的小而完整的文档,而不是两个代码片段(参见下面的示例)
- 正如我在评论中所说,中的名称应该与中
symbolic x coords
使用的相同 。MWE 的一个示例,它生成所需的图表:coordinates
\addplot
\documentclass[a4paper,12pt,twoside,BCOR=10mm]{scrbook}
\usepackage[icelandic]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[
width=.8\linewidth, height=7cm,
ymajorgrids,
title=Flutningstakmarkanir yfir spátímabilið,
enlarge x limits = 0.25, % <--- added
ybar,
xlabel={Sviðsmyndir},
ylabel={Milljón \$ },
xtick=data,
symbolic x coords = {A, B, C},
legend columns=-1, % to set one line legend
legend style={at={(0.5,-0.3)},anchor=center},
]
\addplot coordinates { (A,98) (B,59) (C,43)}; % <---
\addplot coordinates { (A,95) (B,57) (C,42)}; % <---
\legend{text 1, text 2} % <--- added
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
笔记:
- 为了使用符号坐标更加清晰,我将其名称更改为 A、B 和 C。但是,您可以将它们更改为您想要的任何名称,但请记住,在
symbolic x coords
和 中应使用相同的名称coordinates
。 - 我敢打赌缩短你的代码,代码中的更改被标记为
% <---