我正在尝试创建一张与附图一样漂亮的图形。
我的图形有 3 列和 6 行,所以是一个 3x6 矩阵。
头痛:
- 问题 1:
ylabels
和yticklabels
应该只出现在最外面的 y 轴上——称为 y edgeleft,而不是出现在每个子矩阵中。和xlabels
应该xticklabels
只出现在最上面的 x 轴上,称为 x edge top。 - 问题 2:从示例中可以看出,x 标签非常长。
- 问题 3:如何容纳具有负值的子条形图。是否可以使中间线可见,以便轻松划分正条形和负条形?
这次我确实有 MWE。虽然很乱。抱歉。
\documentclass[a4paper,10pt]{article}
\usepackage[left=1.5cm,top=2cm,right=1.5cm,bottom=1.5cm]{geometry}
\usepackage[table,usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.14}
\pgfplotsset{
width=\textwidth,
}
\definecolor{forestgreen(web)}{rgb}{0.13, 0.55, 0.13}
\definecolor{uclagold}{rgb}{1.0, 0.7, 0.0}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group name=my plots,
group size=3 by 6,
xlabels at=edge bottom,
ylabels at=edge left,
horizontal sep=0pt,
vertical sep=0pt,
/pgf/bar width=15pt},
width=5.5cm,
height=5.5cm,
axis lines*=left,
xbar,
enlarge y limits = 0.5,
symbolic y coords={{Non-Poor}, {Moderately Poor}, {Extremely Poor}},
yticklabels={Non-Poor, Moderately Poor, Extremely Poor},
xtick=data,
ylabel style={align=center},
ylabel=A very long\\long\\text as a label,
xlabel style={text width=2.5cm},
xlabel=This is a long xlabel,
xmajorgrids, tick align=inside,
y axis line style={opacity=0},
major grid style={draw=white},
tickwidth=0pt,
xmin=0,
xmax=0.8,
point meta={x*100},
nodes near coords={\tiny\pgfmathprintnumber\pgfplotspointmeta\%},
nodes near coords align={horizontal}
]
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.22,{Non-Poor}) (0.35,{Moderately Poor}) (0.44,{Extremely Poor})};
\nextgroupplot
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.39,{Non-Poor}) (0.28,{Moderately Poor}) (0.09,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.65,{Non-Poor}) (0.52,{Moderately Poor}) (0.20,{Extremely Poor})};
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.18,{Non-Poor}) (0.27,{Moderately Poor}) (0.18,{Extremely Poor})};
\nextgroupplot
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.13,{Non-Poor}) (0.10,{Moderately Poor}) (0.05,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.49,{Non-Poor}) (0.43,{Moderately Poor}) (0.17,{Extremely Poor})};
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.38,{Non-Poor}) (0.35,{Moderately Poor}) (0.15,{Extremely Poor})};
\nextgroupplot
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.05,{Non-Poor}) (0.08,{Moderately Poor}) (0.00,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.35,{Non-Poor}) (0.44,{Moderately Poor}) (0.10,{Extremely Poor})};
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.42,{Non-Poor}) (0.43,{Moderately Poor}) (0.29,{Extremely Poor})};
\nextgroupplot
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.14,{Non-Poor}) (0.13,{Moderately Poor}) (0.05,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.68,{Non-Poor}) (0.52,{Moderately Poor}) (0.13,{Extremely Poor})};
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.04,{Non-Poor}) (0.34,{Moderately Poor}) (0.24,{Extremely Poor})};
\nextgroupplot
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.48,{Non-Poor}) (0.37,{Moderately Poor}) (0.20,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.56,{Non-Poor}) (0.52,{Moderately Poor}) (0.35,{Extremely Poor})};
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.50,{Non-Poor}) (0.44,{Moderately Poor}) (0.34,{Extremely Poor})};
\nextgroupplot
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.40,{Non-Poor}) (0.31,{Moderately Poor}) (0.20,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.76,{Non-Poor}) (0.66,{Moderately Poor}) (0.42,{Extremely Poor})};
\end{groupplot}
\end{tikzpicture}
\end{document}
答案1
希望这可以帮助,
输出
代码
\documentclass[12pt,tikz]{standalone}
%\documentclass[a4paper,10pt]{article}
%\usepackage[left=1.5cm,top=2cm,right=1.5cm,bottom=1.5cm]{geometry}
%\usepackage[table,usenames,dvipsnames]{xcolor}
%\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.14}
\pgfplotsset{
width=\textwidth,
}
\definecolor{forestgreen(web)}{rgb}{0.13, 0.55, 0.13}
\definecolor{uclagold}{rgb}{1.0, 0.7, 0.0}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}
[
group style=
{
group name=my plots,
group size=3 by 6,
%horizontal sep=0pt,
vertical sep=0pt,
/pgf/bar width=15pt,
xticklabels at=edge top,
yticklabels at=edge left,
xlabels at=edge bottom,
ylabels at=edge left,
},
% limits settings
xmin=-.2,
xmax=0.8,
width=5.5cm,
height=5.5cm,
enlarge y limits=0.5,
axis lines*=left,
xbar,
% axis lines
axis x line=top,
axis line style={-},
y axis line style={opacity=0},
major grid style={draw=gray},
% labels
symbolic y coords={{Non-Poor}, {Moderately Poor}, {Extremely Poor}},
ytick=data, %thanks to @Torbjørn T.
ytick style={draw=none},
ylabel style={align=center},
ylabel=A very long\\long\\text as a label,
xlabel=This is a long xlabel,
xmajorgrids,
every axis x label/.style=
{
at={(rel axis cs:0.5,-.2)},
},
% ticks
typeset ticklabels with strut,
xtick distance = .2,
tick align=inside,
%nodes near coords
nodes near coords={\tiny\pgfmathprintnumber\pgfplotspointmeta\%},
nodes near coords style={fill=white},
nodes near coords align={horizontal},
point meta={x*100},
]
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.22,{Non-Poor}) (0.35,{Moderately Poor}) (0.44,{Extremely Poor})};
\nextgroupplot
%\addplot[draw=uclagold,fill=uclagold] coordinates{(0.39,{Non-Poor}) (0.28,{Moderately Poor}) (0.09,{Extremely Poor})};
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.39,{Non-Poor}) (-0.18,{Moderately Poor}) (0.09,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.65,{Non-Poor}) (0.52,{Moderately Poor}) (0.20,{Extremely Poor})};
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.18,{Non-Poor}) (0.27,{Moderately Poor}) (0.18,{Extremely Poor})};
\nextgroupplot
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.13,{Non-Poor}) (0.10,{Moderately Poor}) (0.05,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.49,{Non-Poor}) (0.43,{Moderately Poor}) (0.17,{Extremely Poor})};
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.38,{Non-Poor}) (0.35,{Moderately Poor}) (0.15,{Extremely Poor})};
\nextgroupplot
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.05,{Non-Poor}) (0.08,{Moderately Poor}) (0.00,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.35,{Non-Poor}) (0.44,{Moderately Poor}) (0.10,{Extremely Poor})};
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.42,{Non-Poor}) (0.43,{Moderately Poor}) (0.29,{Extremely Poor})};
\nextgroupplot
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.14,{Non-Poor}) (0.13,{Moderately Poor}) (0.05,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.68,{Non-Poor}) (0.52,{Moderately Poor}) (0.13,{Extremely Poor})};
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.04,{Non-Poor}) (0.34,{Moderately Poor}) (0.24,{Extremely Poor})};
\nextgroupplot
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.48,{Non-Poor}) (0.37,{Moderately Poor}) (0.20,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.56,{Non-Poor}) (0.52,{Moderately Poor}) (0.35,{Extremely Poor})};
\nextgroupplot
\addplot[draw=blue,fill=blue] coordinates{(0.50,{Non-Poor}) (0.44,{Moderately Poor}) (0.34,{Extremely Poor})};
\nextgroupplot
\addplot[draw=uclagold,fill=uclagold] coordinates{(0.40,{Non-Poor}) (0.31,{Moderately Poor}) (0.20,{Extremely Poor})};
\nextgroupplot
\addplot[draw=forestgreen(web),fill=forestgreen(web)] coordinates{(0.76,{Non-Poor}) (0.66,{Moderately Poor}) (0.42,{Extremely Poor})};
\end{groupplot}
\end{tikzpicture}
\end{document}
干杯,