我有一组ybar stacked
运行良好的两幅图,但我需要将其转换为类似的两幅xbar stacked
图配置。这是一组软件工具在不同输入数据集上的运行时间和内存消耗。
因此,我交换了所有我能找到的垂直/水平和 x/y 对,并转置了表格,但结果是错误的:它似乎有点工作,但只显示前七行,它们之间有很大的间距。
这是代码:
\documentclass{article}
\usepackage{xparse}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{colorbrewer}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.13}
\begin{document}
\pgfplotsset{cycle list/Reds-7}
\newcommand\tools%
{first, second, third, fourth, fifth, sixth, seventh}
\pgfplotstableread{time-benchmarks.txt}\timedata
\pgfplotstabletranspose[colnames from=group]{\timedata}{\timedata}
\pgfplotstableread{memory-benchmarks.txt}\memorydata
\pgfplotstabletranspose[colnames from=group]{\memorydata}{\memorydata}
\NewDocumentCommand\datapoints{sm}{
\foreach \tool in \tools {
\addplot table[y expr=\coordindex, x={\tool}] {#2};
\IfBooleanTF{#1}{
\expandafter\addlegendentry\expandafter{\tool}
}{}
}
}
\begin{tikzpicture}
\begin{groupplot}[
% group size and spacing
group style={
group size=2 by 1,
y descriptions at=edge left,
horizontal sep=2ex
},
% Stacked bar plot
xbar stacked,
% Size and spacing
height=16cm,
width=6cm,
/pgf/bar width=1.8ex,
enlarge y limits=0.02,
enlarge x limits=0,
% Bar style
cycle list name=Reds-7,
every axis plot/.append style={fill},
axis line style={
draw=none
},
% Axis ticks and descriptions
xtick=\empty,
ytick=data,
yticklabels from table={\memorydata}{group},
tick label style={
font=\ttfamily\scriptsize,
%rotate=-90
},
tick style={
draw=none
},
% Legend
legend style={
legend columns=4,
anchor=west,
at={(0,-1cm)},
draw=none,
font=\ttfamily,
/tikz/every even column/.append style={column sep=1em}
}
]
\nextgroupplot
\datapoints*{\timedata}
\nextgroupplot
\datapoints{\memorydata}
\end{groupplot}
\end{tikzpicture}
\end{document}
输出结果如下:
我究竟做错了什么?
这里我粘贴了两个要另存为time-benchmarks.txt
和 的示例数据文件memory-benchmarks.txt
。第一个:
group first second third fourth fifth sixth seventh
set01 0.004111 0.009152 0.045724 0.005717 0.021879 0.002881 0.002945
set02 0.008477 0.211507 0.053515 0.007908 990.668120 249.920847 217.070817
set03 0.004044 0.008985 0.044974 0.005878 0.018385 0.002912 0.002863
set04 2.075985 1306.278378 7.665451 1760.381948 1104.620070 1143.079517 1226.006896
set05 1.897231 1368.994888 4.899534 1773.628201 1016.592014 1186.307643 1245.391219
set06 2.137058 1317.487465 9.685547 1800.055000 1134.715376 1178.582506 1167.383633
set07 4.266404 1319.283929 19.259639 1800.048824 1178.716000 1403.177228 1380.575205
set08 3.307151 1425.309175 0.118215 1800.057647 920.797998 1434.119709 1694.171620
set09 0.818796 1177.283560 20.773745 1800.053529 1255.335457 1104.185453 1178.818483
set10 1.627116 1179.517775 41.466340 1800.051176 1273.744990 1425.870249 1588.298746
set11 7.371055 1311.447020 0.096659 1800.055294 1138.917764 1407.002186 1699.648235
set12 0.009644 547.394445 0.056474 1129.883679 1133.682485 1136.452059 1094.560324
set13 0.011048 1361.575559 1200.070602 1666.749689 1200.091553 1200.054252 1200.086195
set14 1317.986718 1305.776547 1308.976817 1700.055894 904.945922 1500.754501 1408.624460
set15 0.036568 3.715888 1.987156 1660.551121 1496.753129 794.032976 347.069509
set16 0.129680 29.559169 9.330906 984.837292 1800.050316 635.171478 635.082265
set17 0.007284 0.110542 0.063316 1139.871041 0.061051 0.045342 106.208491
set18 0.016777 0.531091 0.056427 591.504852 0.649931 445.751227 591.448025
set19 419.720425 0.141559 562.409300 1094.588491 785.076416 304.396500 225.651039
set20 380.819979 0.067163 30.309266 709.116910 789.208049 354.451747 247.732775
第二:
group first second third fourth fifth sixth seventh
set01 0.000000 0.000000 33936.000000 0.000000 11311.600000 0.000000 0.000000
set02 34564.444444 69238.777778 50275.555556 0.000000 349995.222222 106615.888889 78532.333333
set03 0.000000 0.000000 22624.000000 0.000000 4524.640000 0.000000 0.000000
set04 67432.352941 196224.588235 146219.794118 116608.411765 414637.647059 407371.882353 100848.529412
set05 69782.941176 207916.000000 150164.882353 118411.882353 393563.000000 414367.529412 101667.823529
set06 67878.470588 258053.764706 203845.058824 117483.764706 415750.588235 415770.000000 101679.294118
set07 135756.941176 252071.294118 321186.588235 117485.411765 416749.176471 471732.235294 112459.058824
set08 143374.823529 154586.117647 106465.882353 120354.823529 376624.941176 458422.823529 112413.176471
set09 63573.176471 219101.294118 184737.647059 115778.000000 447701.764706 404212.823529 98374.705882
set10 127146.352941 200706.117647 302934.117647 115816.705882 460615.764706 471448.705882 112311.058824
set11 133180.705882 146871.058824 99811.764706 118712.705882 427384.000000 456427.058824 119488.470588
set12 37706.666667 73347.555556 41896.296296 77273.777778 332006.222222 341931.555556 79679.111111
set13 4189.629630 167544.296296 392738.518519 168429.629630 368290.518519 363526.370370 97891.407407
set14 300971.777778 184636.444444 422871.333333 116328.444444 331805.777778 447640.444444 175552.000000
set15 40440.400000 92626.780000 119374.080000 161314.320000 517330.040000 482399.760000 116181.390000
set16 39889.684211 117088.715789 248812.505263 92251.326316 528166.463158 237964.400000 78447.157895
set17 8484.000000 56599.633333 56128.300000 128058.716667 50902.200000 9520.183333 15131.166667
set18 19392.000000 105585.257143 49423.885714 50515.514286 110895.885714 176454.257143 38508.000000
set19 305131.789474 62638.842105 132970.000000 273445.421053 360182.631579 258869.894737 213830.315789
set20 308996.421053 47629.473684 86328.421053 265038.842105 361456.736842 268400.000000 217749.368421
答案1
您所做的一切都是正确的,而不是转置数据表。通过删除这两行,您将获得所需的结果。
% used PGFPlots v1.16
\begin{filecontents*}{time-benchmarks.txt}
group first second third fourth fifth sixth seventh
set01 0.004111 0.009152 0.045724 0.005717 0.021879 0.002881 0.002945
set02 0.008477 0.211507 0.053515 0.007908 990.668120 249.920847 217.070817
set03 0.004044 0.008985 0.044974 0.005878 0.018385 0.002912 0.002863
set04 2.075985 1306.278378 7.665451 1760.381948 1104.620070 1143.079517 1226.006896
set05 1.897231 1368.994888 4.899534 1773.628201 1016.592014 1186.307643 1245.391219
set06 2.137058 1317.487465 9.685547 1800.055000 1134.715376 1178.582506 1167.383633
set07 4.266404 1319.283929 19.259639 1800.048824 1178.716000 1403.177228 1380.575205
set08 3.307151 1425.309175 0.118215 1800.057647 920.797998 1434.119709 1694.171620
set09 0.818796 1177.283560 20.773745 1800.053529 1255.335457 1104.185453 1178.818483
set10 1.627116 1179.517775 41.466340 1800.051176 1273.744990 1425.870249 1588.298746
set11 7.371055 1311.447020 0.096659 1800.055294 1138.917764 1407.002186 1699.648235
set12 0.009644 547.394445 0.056474 1129.883679 1133.682485 1136.452059 1094.560324
set13 0.011048 1361.575559 1200.070602 1666.749689 1200.091553 1200.054252 1200.086195
set14 1317.986718 1305.776547 1308.976817 1700.055894 904.945922 1500.754501 1408.624460
set15 0.036568 3.715888 1.987156 1660.551121 1496.753129 794.032976 347.069509
set16 0.129680 29.559169 9.330906 984.837292 1800.050316 635.171478 635.082265
set17 0.007284 0.110542 0.063316 1139.871041 0.061051 0.045342 106.208491
set18 0.016777 0.531091 0.056427 591.504852 0.649931 445.751227 591.448025
set19 419.720425 0.141559 562.409300 1094.588491 785.076416 304.396500 225.651039
set20 380.819979 0.067163 30.309266 709.116910 789.208049 354.451747 247.732775
\end{filecontents*}
\begin{filecontents*}{memory-benchmarks.txt}
group first second third fourth fifth sixth seventh
set01 0.000000 0.000000 33936.000000 0.000000 11311.600000 0.000000 0.000000
set02 34564.444444 69238.777778 50275.555556 0.000000 349995.222222 106615.888889 78532.333333
set03 0.000000 0.000000 22624.000000 0.000000 4524.640000 0.000000 0.000000
set04 67432.352941 196224.588235 146219.794118 116608.411765 414637.647059 407371.882353 100848.529412
set05 69782.941176 207916.000000 150164.882353 118411.882353 393563.000000 414367.529412 101667.823529
set06 67878.470588 258053.764706 203845.058824 117483.764706 415750.588235 415770.000000 101679.294118
set07 135756.941176 252071.294118 321186.588235 117485.411765 416749.176471 471732.235294 112459.058824
set08 143374.823529 154586.117647 106465.882353 120354.823529 376624.941176 458422.823529 112413.176471
set09 63573.176471 219101.294118 184737.647059 115778.000000 447701.764706 404212.823529 98374.705882
set10 127146.352941 200706.117647 302934.117647 115816.705882 460615.764706 471448.705882 112311.058824
set11 133180.705882 146871.058824 99811.764706 118712.705882 427384.000000 456427.058824 119488.470588
set12 37706.666667 73347.555556 41896.296296 77273.777778 332006.222222 341931.555556 79679.111111
set13 4189.629630 167544.296296 392738.518519 168429.629630 368290.518519 363526.370370 97891.407407
set14 300971.777778 184636.444444 422871.333333 116328.444444 331805.777778 447640.444444 175552.000000
set15 40440.400000 92626.780000 119374.080000 161314.320000 517330.040000 482399.760000 116181.390000
set16 39889.684211 117088.715789 248812.505263 92251.326316 528166.463158 237964.400000 78447.157895
set17 8484.000000 56599.633333 56128.300000 128058.716667 50902.200000 9520.183333 15131.166667
set18 19392.000000 105585.257143 49423.885714 50515.514286 110895.885714 176454.257143 38508.000000
set19 305131.789474 62638.842105 132970.000000 273445.421053 360182.631579 258869.894737 213830.315789
set20 308996.421053 47629.473684 86328.421053 265038.842105 361456.736842 268400.000000 217749.368421
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{xparse}
\usepackage{pgfplotstable}
\usepgfplotslibrary{
colorbrewer,
groupplots,
}
\pgfplotsset{
compat=1.13,
cycle list/Reds-7,
}
\newcommand\tools%
{first, second, third, fourth, fifth, sixth, seventh}
\pgfplotstableread{time-benchmarks.txt}{\timedata}
\pgfplotstableread{memory-benchmarks.txt}{\memorydata}
\NewDocumentCommand\datapoints{sm}{
\foreach \tool in \tools {
\addplot table [y expr=\coordindex,x={\tool}] {#2};
\IfBooleanTF{#1}{
\expandafter\addlegendentry\expandafter{\tool}
}{}
}
}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
% group size and spacing
group style={
group size=2 by 1,
y descriptions at=edge left,
horizontal sep=2ex,
},
% Stacked bar plot
xbar stacked,
% Size and spacing
height=9cm,
width=6cm,
/pgf/bar width=1.8ex,
enlarge y limits=0.02,
enlarge x limits=0,
% Bar style
cycle list name=Reds-7,
every axis plot/.append style={
fill,
},
axis line style={
draw=none,
},
% Axis ticks and descriptions
xtick=\empty,
ytick=data,
yticklabels from table={\memorydata}{group},
tick label style={
font=\ttfamily\scriptsize,
%rotate=-90,
},
tick style={
draw=none,
},
% Legend
legend style={
legend columns=4,
anchor=west,
at={(0,-1cm)},
draw=none,
font=\ttfamily,
/tikz/every even column/.append style={
column sep=1em,
},
},
]
\nextgroupplot
\datapoints*{\timedata}
\nextgroupplot
\datapoints{\memorydata}
\end{groupplot}
\end{tikzpicture}
\end{document}