我今天成功创建了我的第一个“条形图”:
通过使用下面给出的代码:
\documentclass[tikz,border=1pt]{standalone}
\usepackage[ngerman]{babel}
\usepackage[ansinew]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{units}
\usetikzlibrary{matrix}
\pgfplotsset{compat=1.10,
saeule-style/.style={
title={\textbf{Bar Chart}},
},
height=6cm,
width=20cm,
grid=major,
grid style={
solid,
ultra thin,
gray
},
xmin=1925,
xmax=1975,
xtick={1925,1930,...,1975},
x tick label style={
rotate=90
},
x tick style={
color=black,
thin
},
ymin=0,
ymax=10,
y tick style={
color=black,
thin
},
xlabel={\textbf{Year}},
ylabel={\textbf{Population}},
legend columns=2,
legend style={
font=\scriptsize,
legend pos=north west,
draw=none,
/tikz/column 2/.style={
column sep=10pt,
}
},
ybar=5pt,
nodes near coords,
/pgf/number format/.cd,
use comma,
set thousands separator={},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[saeule-style]
\addplot[ybar, bar width=0.2cm, fill=blue, draw=black] coordinates {(1930,5) (1940,3) (1950,4) (1960,5) (1970,7)};
\addplot[ybar, bar width=0.2cm, fill=red, draw=black] coordinates {(1930,3) (1940,4) (1950,4) (1960,4) (1970,6)};
\legend{Far,Near}
\end{axis}
\end{tikzpicture}
\end{document}
如您所见,我添加了 x 和 y 轴。正如我所料,y 轴被绘制出来了在图表。不幸的是,xtics 没有。似乎有什么东西给了它们一种“外部”选项。
你们中有人知道我该如何放置它们吗?在图表?
提前谢谢你,eniem
编辑:嗯...现在尝试使用“xtick align=inside”选项,但它不起作用。有人知道为什么吗?
\documentclass[tikz,border=1pt]{standalone}
\usepackage[ngerman]{babel}
\usepackage[ansinew]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{units}
\pgfplotsset{compat=newest,
saeule-style/.style={
title={\textbf{Bar Chart}},
},
height=6cm,
width=20cm,
grid=major,
grid style={
solid,
ultra thin,
gray
},
xmin=1925,
xmax=1975,
xtick={1925,1930,...,1975},
xtick align=inside,
xticklabel style={
rotate=90
},
xtick style={
color=black,
thin
},
ymin=0,
ymax=10,
ytick align=inside,
y tick style={
color=black,
thin
},
xlabel={\textbf{Year}},
ylabel={\textbf{Population}},
legend columns=2,
legend style={
font=\scriptsize,
legend pos=north west,
draw=none,
/tikz/column 2/.style={
column sep=10pt,
}
},
ybar=5pt,
/pgf/number format/.cd,
use comma,
set thousands separator={},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[saeule-style]
\addplot[ybar, bar width=6pt, fill=blue, draw=black] coordinates {(1930,5) (1940,3) (1950,4) (1960,5) (1970,7)};
\addlegendentry{Far}
\addplot[ybar, bar width=6pt, fill=red, draw=black] coordinates {(1930,3) (1940,4) (1950,4) (1960,4) (1970,6)};
\addlegendentry{Near}
\end{axis}
\end{tikzpicture}
\end{document}
提前谢谢你! eniem
答案1
引用手册ybar
;
它改变图例,在轴线外绘制刻度,并绘制彼此相邻的多个 \addplot 参数;......
所以这是意料之中的事。不过,您可以通过一些小的调整来覆盖这一点,xtick align=inside
正如@Jake 在评论中提醒的那样,它会自动为您完成。
\documentclass[border=1pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10,
xtick={1925,1930,...,1975},
x tick label style={rotate=90,xshift=-4mm},
ybar=5pt,
xtick style={/pgfplots/major tick length=-2mm,draw=black,thick}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[ybar, bar width=0.2cm, fill=blue, draw=black] coordinates {(1930,5) (1940,3) (1950,4) (1960,5) (1970,7)};
\addplot[ybar, bar width=0.2cm, fill=red, draw=black] coordinates {(1930,3) (1940,4) (1950,4) (1960,4) (1970,6)};
\end{axis}
\end{tikzpicture}
\end{document}