pgfplots:循环条形图条形颜色以提高可读性,从文件加载数据

pgfplots:循环条形图条形颜色以提高可读性,从文件加载数据

我有一个置信区间样式的条形图,如下所示。有没有办法改变它,使每个条形(和每个标签)都有不同的颜色(来自颜色循环)以提高可读性?当我从文件加载数据时,我能找到的所有答案都使用 foreach 或其他构造。

nrick 图表

\documentclass[tikz]{standalone}
\usepackage{pgfplots,filecontents}
\pgfplotsset{compat=newest}

\begin{filecontents}{ntrick.dat}
CHAMP   X   Y   YL  YH
YASUO   1   0.4107142857    0.1190484959    0.1305120931
AURELION-SOL    2   0.2222222222    0.1161512673    0.1853507324
DARIUS  3   0.1941747573    0.06481265042   0.08680517491
KOG-MAW 4   0.1785714286    0.09978757028   0.1773464869
TALIYAH 5   0.1666666667    0.07971256627   0.1291143337
GRAGAS  6   0.156626506 0.06274727049   0.09312680322
ZED 7   0.1492537313    0.06611188926   0.1041524436
NIDALEE 8   0.1304347826    0.06026929692   0.09925036651
URGOT   9   0.1282051282    0.07217777872   0.1388553622
OLAF    10  0.12    0.05556701031   0.0925984252
MORDEKAISER 11  0.1194029851    0.04455530856   0.06576953551
IVERN   12  0.1136363636    0.06410928774   0.1261579878
ORIANNA 13  0.1111111111    0.05918159294   0.1108383782
ANIVIA  14  0.1111111111    0.03129912354   0.04153724967
FIORA   15  0.1076923077    0.04245156691   0.06497205726
KHA-ZIX 16  0.1058823529    0.04917058778   0.08325467346
SINGED  17  0.1027777778    0.02728978089   0.03567787553
TRYNDAMERE  18  0.09979633401   0.02348415136   0.02969794746
REK-SAI 19  0.09523809524   0.06871988644   0.1939077434
JARVAN-IV   20  0.09090909091   0.04412322921   0.07834657977
TALON   21  0.08450704225   0.03550617218   0.05739509477
IRELIA  22  0.08438818565   0.02909757023   0.0423561962
RAMMUS  23  0.0843373494    0.04288454022   0.07965976404
JAYCE   24  0.08421052632   0.03171308747   0.04819352141
JANNA   25  0.08316008316   0.02149690193   0.02810249116
RIVEN   26  0.08227114716   0.01653443314   0.02023694848
KALISTA 27  0.08108108108   0.05312101468   0.1319288439
KASSADIN    28  0.08108108108   0.05312101468   0.1319288439
KLED    29  0.07894736842   0.0422638629    0.08278198352
FIZZ    30  0.07894736842   0.03321198596   0.05397044569
RUMBLE  31  0.0780141844    0.03389651291   0.05628097579
EVELYNN 32  0.07751937984   0.02678004714   0.03917686682
CASSIOPEIA  33  0.07692307692   0.05041616285   0.1262906544
TWISTED-FATE    34  0.07446808511   0.03792996909   0.07134568192
KARTHUS 35  0.07246376812   0.0411164589    0.08621220606
JAX 36  0.0701754386    0.02652196482   0.04076629401
SHACO   37  0.06944444444   0.01803462731   0.0237397103
AKALI   38  0.06784660767   0.02221503378   0.03189974224
GANGPLANK   39  0.06443298969   0.02041233594   0.02895290043
GALIO   40  0.0625  0.05138094269   0.2207926837
DRAVEN  41  0.06162464986   0.0154014113    0.020093429
ELISE   42  0.06    0.03938540364   0.1021734337
TEEMO   43  0.05967450271   0.01687076741   0.02294629924
SHEN    44  0.0593220339    0.03029263859   0.05808131626
KINDRED 45  0.05882352941   0.03861725655   0.10042521
VEIGAR  46  0.05479452055   0.02677117999   0.04959930475
VEL-KOZ 47  0.05421686747   0.01965055354   0.02984893564
AATROX  48  0.05263157895   0.03457161085   0.09106619766
UDYR    49  0.05154639175   0.02933066833   0.06349869782
RENEKTON    50  0.05138339921   0.02111228283   0.03453227035
FIDDLESTICKS    51  0.05058365759   0.02078745972   0.03402520877
SKARNER 52  0.05    0.03617962285   0.1150417082
TARIC   53  0.05    0.02444971  0.04555204298
BLITZCRANK  54  0.04838709677   0.01884791975   0.02990391825
NOCTURNE    55  0.04807692308   0.02736918613   0.05956655138
XERATH  56  0.048   0.02581829682   0.05277237376
AHRI    57  0.04761904762   0.02329534513   0.04352172416
RYZE    58  0.046875    0.02521745111   0.05162375989
SION    59  0.046875    0.02202104919   0.0397979158
KAYLE   60  0.0462962963    0.02095782855   0.03681422832
SEJUANI 61  0.04545454545   0.03289969909   0.1058981698
ANNIE   62  0.04545454545   0.0181882452    0.02938740412
MASTER-YI   63  0.04545454545   0.01728501618   0.02709939324
SORAKA  64  0.04387990762   0.01561125405   0.0236335255
GNAR    65  0.04347826087   0.03147306728   0.1018469683
HEIMERDINGER    66  0.04347826087   0.01903019573   0.0326866588
LULU    67  0.04329004329   0.01960870772   0.03455066835
PANTHEON    68  0.04237288136   0.01919678035   0.03385662534
VLADIMIR    69  0.04210526316   0.02561175718   0.06120503817
LISSANDRA   70  0.04166666667   0.03427340131   0.1607555958
NUNU    71  0.03846153846   0.03163972948   0.1504702315
SYNDRA  72  0.03846153846   0.03163972948   0.1504702315
LEE-SIN 73  0.03773584906   0.02296524095   0.05529969278
POPPY   74  0.03738317757   0.02275151504   0.05481869533
SWAIN   75  0.03731343284   0.01692164376   0.02999879395
WUKONG  76  0.0367816092    0.01401665416   0.02212664086
KENNEN  77  0.03636363636   0.02213355578   0.05342449783
MALZAHAR    78  0.03636363636   0.02213355578   0.05342449783
DIANA   79  0.03535353535   0.01812488744   0.03581188416
ZAC 80  0.03465346535   0.01776801123   0.0351374375
CHO-GATH    81  0.03355704698   0.01914015326   0.04258779118
TWITCH  82  0.03355704698   0.01914015326   0.04258779118
MAOKAI  83  0.03333333333   0.02742489534   0.1333741806
ZILEAN  84  0.03300330033   0.01497977204   0.02667319641
HECARIM 85  0.0303030303    0.02493375821   0.1228875864
TAHM-KENCH  86  0.0303030303    0.01994457841   0.05503520086
CORKI   87  0.02941176471   0.02420099278   0.1197472575
SHYVANA 88  0.02884615385   0.01898814955   0.05255561546
MISS-FORTUNE    89  0.02857142857   0.01346866316   0.02482878681
AMUMU   90  0.02830188679   0.01863077815   0.05162511675
YORICK  91  0.02777777778   0.02012691631   0.06796580391
JHIN    92  0.02777777778   0.01585583158   0.03559117863
BRAND   93  0.02713178295   0.01392843003   0.02780376949
NASUS   94  0.02688172043   0.01534615471   0.03449402517
BARD    95  0.02659574468   0.01518346633   0.03414316837
SONA    96  0.02616516762   0.0075710332    0.01053846419
EKKO    97  0.02608695652   0.01407759625   0.02964868666
GRAVES  98  0.02469135802   0.01789391311   0.06093755395
VIKTOR  99  0.0243902439    0.02007182363   0.1015632262
QUINN   100 0.02395209581   0.01459922489   0.03600836211
THRESH  101 0.02151639344   0.007401003996  0.0111529219
NAMI    102 0.02080624187   0.007959442523  0.01272334431
ALISTAR 103 0.01973684211   0.01300231219   0.03667993033
LEONA   104 0.01946472019   0.007448633761  0.01191928365
XIN-ZHAO    105 0.01915708812   0.01094737104   0.0248968875
ZYRA    106 0.01893095768   0.00707839643   0.01117684306
KARMA   107 0.01826484018   0.01113976474   0.0277491751
VAYNE   108 0.01739130435   0.0126090522    0.04381007213
GAREN   109 0.015   0.009885854816  0.02816648056
DR-MUNDO    110 0.01470588235   0.01210526575   0.06400572126
VOLIBEAR    111 0.01449275362   0.009551973694  0.02724416193
MALPHITE    112 0.01307189542   0.00947983485   0.03333298359
EZREAL  113 0.0125  0.009065400844  0.03192626158
VARUS   114 0.01149425287   0.009462397449  0.05077923199
VI  115 0.01123595506   0.009249822963  0.04969798316
ILLAOI  116 0.01052631579   0.008665787426  0.04671377745
MORGANA 117 0.009538950715  0.00516009818   0.01111468513
JINX    118 0.008645533141  0.005701054612  0.01646139386
BRAUM   119 0.008547008547  0.006200021825  0.02207584684
CAITLYN 120 0.006756756757  0.005563056588  0.03052121355
NAUTILUS    121 0.005730659026  0.004157729224  0.01492054801
LUX 122 0.002364066194  0.001946639235  0.01090414574
CAMILLE 123 0   0   0.6576280471
AZIR    124 0   0   0.1245590371
SIVIR   125 0   0   0.1135170914
LEBLANC 126 0   0   0.1015179062
RENGAR  127 0   0   0.08380161251
ZIGGS   128 0   0   0.08201257002
TRUNDLE 129 0   0   0.07707617733
WARWICK 130 0   0   0.06314100878
LUCIAN  131 0   0   0.06017393048
TRISTANA    132 0   0   0.05273909414
KATARINA    133 0   0   0.03259969315
ASHE    134 0   0   0.03028659367
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\sffamily
\begin{axis}[
    xtick=data,% crucial line for the xticklabels directive
    xticklabels from table={ntrick.dat}{CHAMP},
    xticklabel style={rotate=90,font=\tiny},
    xmin=-2,
    xmax=137,
    ymin=-0.02,
    ymax=0.6,
    height=480,
    width=820
]
\draw
    (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) --
    (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);

\addplot [
    only marks, mark=o,
    error bars/.cd, y dir = minus, y explicit
]
table [
    x=X,
    y=Y,
    y error=YL
] {ntrick.dat};

\addplot [
    only marks, mark=o,
    error bars/.cd, y dir = plus, y explicit
]
table [
    x=X,
    y=Y,
    y error=YH
] {ntrick.dat};

\end{axis}
\end{tikzpicture}
\end{document}

答案1

在这里我没有回答改变每个数据点颜色的直接问题,但我不明白这如何提高可读性。

但关于主要的为了提高可读性,我建议两种方案。首先,可以每 5 个条目添加垂直线以将它们分组,这样就可以有一些指导线来引导数据点,或者在背景中添加彩色矩形,其目的同样是作为我在组中的位置的定位。

我个人更喜欢第二种选择,因为它不会像纯垂直线那样造成太大的干扰,纯垂直线可能会被误解为较大的误差线,因此如下所示。

有关其工作原理的详细信息,请查看代码中的注释。

% used PGFPlots v1.14
    \begin{filecontents}{ntrick.dat}
        CHAMP   X   Y   YL  YH
        YASUO   1   0.4107142857    0.1190484959    0.1305120931
        AURELION-SOL    2   0.2222222222    0.1161512673    0.1853507324
        DARIUS  3   0.1941747573    0.06481265042   0.08680517491
        KOG-MAW 4   0.1785714286    0.09978757028   0.1773464869
        TALIYAH 5   0.1666666667    0.07971256627   0.1291143337
        GRAGAS  6   0.156626506 0.06274727049   0.09312680322
        ZED 7   0.1492537313    0.06611188926   0.1041524436
        NIDALEE 8   0.1304347826    0.06026929692   0.09925036651
        URGOT   9   0.1282051282    0.07217777872   0.1388553622
        OLAF    10  0.12    0.05556701031   0.0925984252
        MORDEKAISER 11  0.1194029851    0.04455530856   0.06576953551
        IVERN   12  0.1136363636    0.06410928774   0.1261579878
        ORIANNA 13  0.1111111111    0.05918159294   0.1108383782
        ANIVIA  14  0.1111111111    0.03129912354   0.04153724967
        FIORA   15  0.1076923077    0.04245156691   0.06497205726
        KHA-ZIX 16  0.1058823529    0.04917058778   0.08325467346
        SINGED  17  0.1027777778    0.02728978089   0.03567787553
        TRYNDAMERE  18  0.09979633401   0.02348415136   0.02969794746
        REK-SAI 19  0.09523809524   0.06871988644   0.1939077434
        JARVAN-IV   20  0.09090909091   0.04412322921   0.07834657977
        TALON   21  0.08450704225   0.03550617218   0.05739509477
        IRELIA  22  0.08438818565   0.02909757023   0.0423561962
        RAMMUS  23  0.0843373494    0.04288454022   0.07965976404
        JAYCE   24  0.08421052632   0.03171308747   0.04819352141
        JANNA   25  0.08316008316   0.02149690193   0.02810249116
        RIVEN   26  0.08227114716   0.01653443314   0.02023694848
        KALISTA 27  0.08108108108   0.05312101468   0.1319288439
        KASSADIN    28  0.08108108108   0.05312101468   0.1319288439
        KLED    29  0.07894736842   0.0422638629    0.08278198352
        FIZZ    30  0.07894736842   0.03321198596   0.05397044569
        RUMBLE  31  0.0780141844    0.03389651291   0.05628097579
        EVELYNN 32  0.07751937984   0.02678004714   0.03917686682
        CASSIOPEIA  33  0.07692307692   0.05041616285   0.1262906544
        TWISTED-FATE    34  0.07446808511   0.03792996909   0.07134568192
        KARTHUS 35  0.07246376812   0.0411164589    0.08621220606
        JAX 36  0.0701754386    0.02652196482   0.04076629401
        SHACO   37  0.06944444444   0.01803462731   0.0237397103
        AKALI   38  0.06784660767   0.02221503378   0.03189974224
        GANGPLANK   39  0.06443298969   0.02041233594   0.02895290043
        GALIO   40  0.0625  0.05138094269   0.2207926837
        DRAVEN  41  0.06162464986   0.0154014113    0.020093429
        ELISE   42  0.06    0.03938540364   0.1021734337
        TEEMO   43  0.05967450271   0.01687076741   0.02294629924
        SHEN    44  0.0593220339    0.03029263859   0.05808131626
        KINDRED 45  0.05882352941   0.03861725655   0.10042521
        VEIGAR  46  0.05479452055   0.02677117999   0.04959930475
        VEL-KOZ 47  0.05421686747   0.01965055354   0.02984893564
        AATROX  48  0.05263157895   0.03457161085   0.09106619766
        UDYR    49  0.05154639175   0.02933066833   0.06349869782
        RENEKTON    50  0.05138339921   0.02111228283   0.03453227035
        FIDDLESTICKS    51  0.05058365759   0.02078745972   0.03402520877
        SKARNER 52  0.05    0.03617962285   0.1150417082
        TARIC   53  0.05    0.02444971  0.04555204298
        BLITZCRANK  54  0.04838709677   0.01884791975   0.02990391825
        NOCTURNE    55  0.04807692308   0.02736918613   0.05956655138
        XERATH  56  0.048   0.02581829682   0.05277237376
        AHRI    57  0.04761904762   0.02329534513   0.04352172416
        RYZE    58  0.046875    0.02521745111   0.05162375989
        SION    59  0.046875    0.02202104919   0.0397979158
        KAYLE   60  0.0462962963    0.02095782855   0.03681422832
        SEJUANI 61  0.04545454545   0.03289969909   0.1058981698
        ANNIE   62  0.04545454545   0.0181882452    0.02938740412
        MASTER-YI   63  0.04545454545   0.01728501618   0.02709939324
        SORAKA  64  0.04387990762   0.01561125405   0.0236335255
        GNAR    65  0.04347826087   0.03147306728   0.1018469683
        HEIMERDINGER    66  0.04347826087   0.01903019573   0.0326866588
        LULU    67  0.04329004329   0.01960870772   0.03455066835
        PANTHEON    68  0.04237288136   0.01919678035   0.03385662534
        VLADIMIR    69  0.04210526316   0.02561175718   0.06120503817
        LISSANDRA   70  0.04166666667   0.03427340131   0.1607555958
        NUNU    71  0.03846153846   0.03163972948   0.1504702315
        SYNDRA  72  0.03846153846   0.03163972948   0.1504702315
        LEE-SIN 73  0.03773584906   0.02296524095   0.05529969278
        POPPY   74  0.03738317757   0.02275151504   0.05481869533
        SWAIN   75  0.03731343284   0.01692164376   0.02999879395
        WUKONG  76  0.0367816092    0.01401665416   0.02212664086
        KENNEN  77  0.03636363636   0.02213355578   0.05342449783
        MALZAHAR    78  0.03636363636   0.02213355578   0.05342449783
        DIANA   79  0.03535353535   0.01812488744   0.03581188416
        ZAC 80  0.03465346535   0.01776801123   0.0351374375
        CHO-GATH    81  0.03355704698   0.01914015326   0.04258779118
        TWITCH  82  0.03355704698   0.01914015326   0.04258779118
        MAOKAI  83  0.03333333333   0.02742489534   0.1333741806
        ZILEAN  84  0.03300330033   0.01497977204   0.02667319641
        HECARIM 85  0.0303030303    0.02493375821   0.1228875864
        TAHM-KENCH  86  0.0303030303    0.01994457841   0.05503520086
        CORKI   87  0.02941176471   0.02420099278   0.1197472575
        SHYVANA 88  0.02884615385   0.01898814955   0.05255561546
        MISS-FORTUNE    89  0.02857142857   0.01346866316   0.02482878681
        AMUMU   90  0.02830188679   0.01863077815   0.05162511675
        YORICK  91  0.02777777778   0.02012691631   0.06796580391
        JHIN    92  0.02777777778   0.01585583158   0.03559117863
        BRAND   93  0.02713178295   0.01392843003   0.02780376949
        NASUS   94  0.02688172043   0.01534615471   0.03449402517
        BARD    95  0.02659574468   0.01518346633   0.03414316837
        SONA    96  0.02616516762   0.0075710332    0.01053846419
        EKKO    97  0.02608695652   0.01407759625   0.02964868666
        GRAVES  98  0.02469135802   0.01789391311   0.06093755395
        VIKTOR  99  0.0243902439    0.02007182363   0.1015632262
        QUINN   100 0.02395209581   0.01459922489   0.03600836211
        THRESH  101 0.02151639344   0.007401003996  0.0111529219
        NAMI    102 0.02080624187   0.007959442523  0.01272334431
        ALISTAR 103 0.01973684211   0.01300231219   0.03667993033
        LEONA   104 0.01946472019   0.007448633761  0.01191928365
        XIN-ZHAO    105 0.01915708812   0.01094737104   0.0248968875
        ZYRA    106 0.01893095768   0.00707839643   0.01117684306
        KARMA   107 0.01826484018   0.01113976474   0.0277491751
        VAYNE   108 0.01739130435   0.0126090522    0.04381007213
        GAREN   109 0.015   0.009885854816  0.02816648056
        DR-MUNDO    110 0.01470588235   0.01210526575   0.06400572126
        VOLIBEAR    111 0.01449275362   0.009551973694  0.02724416193
        MALPHITE    112 0.01307189542   0.00947983485   0.03333298359
        EZREAL  113 0.0125  0.009065400844  0.03192626158
        VARUS   114 0.01149425287   0.009462397449  0.05077923199
        VI  115 0.01123595506   0.009249822963  0.04969798316
        ILLAOI  116 0.01052631579   0.008665787426  0.04671377745
        MORGANA 117 0.009538950715  0.00516009818   0.01111468513
        JINX    118 0.008645533141  0.005701054612  0.01646139386
        BRAUM   119 0.008547008547  0.006200021825  0.02207584684
        CAITLYN 120 0.006756756757  0.005563056588  0.03052121355
        NAUTILUS    121 0.005730659026  0.004157729224  0.01492054801
        LUX 122 0.002364066194  0.001946639235  0.01090414574
        CAMILLE 123 0   0   0.6576280471
        AZIR    124 0   0   0.1245590371
        SIVIR   125 0   0   0.1135170914
        LEBLANC 126 0   0   0.1015179062
        RENGAR  127 0   0   0.08380161251
        ZIGGS   128 0   0   0.08201257002
        TRUNDLE 129 0   0   0.07707617733
        WARWICK 130 0   0   0.06314100878
        LUCIAN  131 0   0   0.06017393048
        TRISTANA    132 0   0   0.05273909414
        KATARINA    133 0   0   0.03259969315
        ASHE    134 0   0   0.03028659367
    \end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % use this `compat' level or higher so you don't need to prefix every
    % TikZ coordinate with `axis cs:'
    \pgfplotsset{compat=1.11}
    % declare the layer background on which later on the rectangles will be placed
    % and set the order of the layers
    \pgfdeclarelayer{background}
    \pgfsetlayers{background,main}
\begin{document}
\begin{tikzpicture}
        \sffamily
    \begin{axis}[
        height=480,
        width=820,
        % I wouldn't use these explicitly here, because if the number of data
        % points changes you have to adjust them ...
%        xmin=-2,
%        xmax=137,
        ymin=-0.02,
        ymax=0.6,
        % ... Insted use `enlarge x limits'.
        enlarge x limits={abs=1},
        xtick=data,% crucial line for the xticklabels directive
        xticklabels from table={ntrick.dat}{CHAMP},
        xticklabel style={rotate=90,font=\tiny},
        % to avoid the nasty "5 \cdot 10^-2".
        yticklabel style={
            /pgf/number format/fixed,
        },
%        % ---------------------------------------------------------------------
%        % one possibility would be to add some "guide lines" only to easier see
%        % to which xtick which data point belongs
%        extra x ticks={5,10,...,150},
%        extra x ticks={5.5,10.5,...,150.5},
%        extra x tick labels={},
%        extra x tick style={
%            grid=major,
%            major tick length=0pt,
%        },
%        % ---------------------------------------------------------------------
        % I realized your zero line with the help of the `extra y ticks' feature
        extra y ticks={0},
        extra y tick labels={},
        extra y tick style={
            grid=major,
            major tick length=0pt,
        },
        only marks,
    ]

        % ---------------------------------------------------------------------
        % my prefered solution is to place filled rectangles in the background
        % each 5 entries to guide the reader to which xtick which data point
        % belongs.
        %
        % We place the rectangles on the previously defined background layer,
        % so they are also placed behind the axis and thus the `xtick's.
        % (Using the `axis on top' feature would also be an option, but that
        %  would cause the created `extra y ticks' line to be on top of the
        %  data points as well which most probably is not desired.)
        \begin{pgfonlayer}{background}
            % I suggest to group data points in steps of 5, so we have to draw
            % colored rectangles each 10 entries with a width of 5.
            % I think it is a matter of taste if the rectangles should be placed
            % *on* the data points or *between* the data points. Here I have
            % chosen the later one.
            \pgfplotsinvokeforeach {5.5,15.5,...,135.5} {
                \fill [black!5] (#1,\pgfkeysvalueof{/pgfplots/ymin})
                    rectangle (#1+5,\pgfkeysvalueof{/pgfplots/ymax});
            }
        \end{pgfonlayer}
        % ---------------------------------------------------------------------

        % you can draw both error bars in one call
        \addplot [
            % because I don't like it that you see the error bar line "through"
            % the data point I changed the mark to the corresponding one that
            % can be filled and filled it white
            mark=*,
            fill=white,
            error bars/.cd,
                y dir = both,
                y explicit,
        ] table [
            x=X,
            y=Y,
            y error minus=YL,
            y error plus =YH,
        ] {ntrick.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

答案2

cycle list name中机制的问题pgfplots(至少就我对手册的理解而言)在于,循环列表元素是根据当前绘图编号选择的,该编号以\addplot和朋友为增量。因此,如果您的轴中只有一个\addplot,则永远不会选择循环列表中的下一个元素。

我确实想出了一个部分的解决方案。但是,它有一些缺点:

  • 它很慢。有些人甚至可能会说非常速度很慢(在我的计算机上编译时间几乎为 30 秒,而您的原始代码编译时间约为 2 秒)。这是因为整个表在每个绘图周期中都会被处理,只有除了一个元素将被丢弃。我必须承认,这很不雅致。如果有人能想出一个不那么笨重的变体,我会非常感兴趣。
  • 它不会循环刻度标签样式。我仔细阅读了pgfplots手册,并尝试了刻度标签和符号坐标,但遗憾的是,事情并没有按照我的意愿发展。这确实有道理,因为刻度标签是为整个设置的axis,因此不存在循环。如果我找到可行的解决方案,我会发布更新,因为这确实是一件相当不错的事情,我同意你的观点。

在实际解决方案之前的最后一件事:片段x filter/.code来自这个答案,并感谢 Stefan Pinnow 提供的错误条形码片段他的回答在这里

无论如何,代码:

\documentclass[tikz]{standalone}
\usepackage{pgfplots,filecontents,pgfplotstable}
\pgfplotsset{compat=newest}

\begin{filecontents}{ntrick.dat}
CHAMP   X   Y   YL  YH
YASUO   1   0.4107142857    0.1190484959    0.1305120931
AURELION-SOL    2   0.2222222222    0.1161512673    0.1853507324
DARIUS  3   0.1941747573    0.06481265042   0.08680517491
KOG-MAW 4   0.1785714286    0.09978757028   0.1773464869
TALIYAH 5   0.1666666667    0.07971256627   0.1291143337
GRAGAS  6   0.156626506 0.06274727049   0.09312680322
ZED 7   0.1492537313    0.06611188926   0.1041524436
NIDALEE 8   0.1304347826    0.06026929692   0.09925036651
URGOT   9   0.1282051282    0.07217777872   0.1388553622
OLAF    10  0.12    0.05556701031   0.0925984252
MORDEKAISER 11  0.1194029851    0.04455530856   0.06576953551
IVERN   12  0.1136363636    0.06410928774   0.1261579878
ORIANNA 13  0.1111111111    0.05918159294   0.1108383782
ANIVIA  14  0.1111111111    0.03129912354   0.04153724967
FIORA   15  0.1076923077    0.04245156691   0.06497205726
KHA-ZIX 16  0.1058823529    0.04917058778   0.08325467346
SINGED  17  0.1027777778    0.02728978089   0.03567787553
TRYNDAMERE  18  0.09979633401   0.02348415136   0.02969794746
REK-SAI 19  0.09523809524   0.06871988644   0.1939077434
JARVAN-IV   20  0.09090909091   0.04412322921   0.07834657977
TALON   21  0.08450704225   0.03550617218   0.05739509477
IRELIA  22  0.08438818565   0.02909757023   0.0423561962
RAMMUS  23  0.0843373494    0.04288454022   0.07965976404
JAYCE   24  0.08421052632   0.03171308747   0.04819352141
JANNA   25  0.08316008316   0.02149690193   0.02810249116
RIVEN   26  0.08227114716   0.01653443314   0.02023694848
KALISTA 27  0.08108108108   0.05312101468   0.1319288439
KASSADIN    28  0.08108108108   0.05312101468   0.1319288439
KLED    29  0.07894736842   0.0422638629    0.08278198352
FIZZ    30  0.07894736842   0.03321198596   0.05397044569
RUMBLE  31  0.0780141844    0.03389651291   0.05628097579
EVELYNN 32  0.07751937984   0.02678004714   0.03917686682
CASSIOPEIA  33  0.07692307692   0.05041616285   0.1262906544
TWISTED-FATE    34  0.07446808511   0.03792996909   0.07134568192
KARTHUS 35  0.07246376812   0.0411164589    0.08621220606
JAX 36  0.0701754386    0.02652196482   0.04076629401
SHACO   37  0.06944444444   0.01803462731   0.0237397103
AKALI   38  0.06784660767   0.02221503378   0.03189974224
GANGPLANK   39  0.06443298969   0.02041233594   0.02895290043
GALIO   40  0.0625  0.05138094269   0.2207926837
DRAVEN  41  0.06162464986   0.0154014113    0.020093429
ELISE   42  0.06    0.03938540364   0.1021734337
TEEMO   43  0.05967450271   0.01687076741   0.02294629924
SHEN    44  0.0593220339    0.03029263859   0.05808131626
KINDRED 45  0.05882352941   0.03861725655   0.10042521
VEIGAR  46  0.05479452055   0.02677117999   0.04959930475
VEL-KOZ 47  0.05421686747   0.01965055354   0.02984893564
AATROX  48  0.05263157895   0.03457161085   0.09106619766
UDYR    49  0.05154639175   0.02933066833   0.06349869782
RENEKTON    50  0.05138339921   0.02111228283   0.03453227035
FIDDLESTICKS    51  0.05058365759   0.02078745972   0.03402520877
SKARNER 52  0.05    0.03617962285   0.1150417082
TARIC   53  0.05    0.02444971  0.04555204298
BLITZCRANK  54  0.04838709677   0.01884791975   0.02990391825
NOCTURNE    55  0.04807692308   0.02736918613   0.05956655138
XERATH  56  0.048   0.02581829682   0.05277237376
AHRI    57  0.04761904762   0.02329534513   0.04352172416
RYZE    58  0.046875    0.02521745111   0.05162375989
SION    59  0.046875    0.02202104919   0.0397979158
KAYLE   60  0.0462962963    0.02095782855   0.03681422832
SEJUANI 61  0.04545454545   0.03289969909   0.1058981698
ANNIE   62  0.04545454545   0.0181882452    0.02938740412
MASTER-YI   63  0.04545454545   0.01728501618   0.02709939324
SORAKA  64  0.04387990762   0.01561125405   0.0236335255
GNAR    65  0.04347826087   0.03147306728   0.1018469683
HEIMERDINGER    66  0.04347826087   0.01903019573   0.0326866588
LULU    67  0.04329004329   0.01960870772   0.03455066835
PANTHEON    68  0.04237288136   0.01919678035   0.03385662534
VLADIMIR    69  0.04210526316   0.02561175718   0.06120503817
LISSANDRA   70  0.04166666667   0.03427340131   0.1607555958
NUNU    71  0.03846153846   0.03163972948   0.1504702315
SYNDRA  72  0.03846153846   0.03163972948   0.1504702315
LEE-SIN 73  0.03773584906   0.02296524095   0.05529969278
POPPY   74  0.03738317757   0.02275151504   0.05481869533
SWAIN   75  0.03731343284   0.01692164376   0.02999879395
WUKONG  76  0.0367816092    0.01401665416   0.02212664086
KENNEN  77  0.03636363636   0.02213355578   0.05342449783
MALZAHAR    78  0.03636363636   0.02213355578   0.05342449783
DIANA   79  0.03535353535   0.01812488744   0.03581188416
ZAC 80  0.03465346535   0.01776801123   0.0351374375
CHO-GATH    81  0.03355704698   0.01914015326   0.04258779118
TWITCH  82  0.03355704698   0.01914015326   0.04258779118
MAOKAI  83  0.03333333333   0.02742489534   0.1333741806
ZILEAN  84  0.03300330033   0.01497977204   0.02667319641
HECARIM 85  0.0303030303    0.02493375821   0.1228875864
TAHM-KENCH  86  0.0303030303    0.01994457841   0.05503520086
CORKI   87  0.02941176471   0.02420099278   0.1197472575
SHYVANA 88  0.02884615385   0.01898814955   0.05255561546
MISS-FORTUNE    89  0.02857142857   0.01346866316   0.02482878681
AMUMU   90  0.02830188679   0.01863077815   0.05162511675
YORICK  91  0.02777777778   0.02012691631   0.06796580391
JHIN    92  0.02777777778   0.01585583158   0.03559117863
BRAND   93  0.02713178295   0.01392843003   0.02780376949
NASUS   94  0.02688172043   0.01534615471   0.03449402517
BARD    95  0.02659574468   0.01518346633   0.03414316837
SONA    96  0.02616516762   0.0075710332    0.01053846419
EKKO    97  0.02608695652   0.01407759625   0.02964868666
GRAVES  98  0.02469135802   0.01789391311   0.06093755395
VIKTOR  99  0.0243902439    0.02007182363   0.1015632262
QUINN   100 0.02395209581   0.01459922489   0.03600836211
THRESH  101 0.02151639344   0.007401003996  0.0111529219
NAMI    102 0.02080624187   0.007959442523  0.01272334431
ALISTAR 103 0.01973684211   0.01300231219   0.03667993033
LEONA   104 0.01946472019   0.007448633761  0.01191928365
XIN-ZHAO    105 0.01915708812   0.01094737104   0.0248968875
ZYRA    106 0.01893095768   0.00707839643   0.01117684306
KARMA   107 0.01826484018   0.01113976474   0.0277491751
VAYNE   108 0.01739130435   0.0126090522    0.04381007213
GAREN   109 0.015   0.009885854816  0.02816648056
DR-MUNDO    110 0.01470588235   0.01210526575   0.06400572126
VOLIBEAR    111 0.01449275362   0.009551973694  0.02724416193
MALPHITE    112 0.01307189542   0.00947983485   0.03333298359
EZREAL  113 0.0125  0.009065400844  0.03192626158
VARUS   114 0.01149425287   0.009462397449  0.05077923199
VI  115 0.01123595506   0.009249822963  0.04969798316
ILLAOI  116 0.01052631579   0.008665787426  0.04671377745
MORGANA 117 0.009538950715  0.00516009818   0.01111468513
JINX    118 0.008645533141  0.005701054612  0.01646139386
BRAUM   119 0.008547008547  0.006200021825  0.02207584684
CAITLYN 120 0.006756756757  0.005563056588  0.03052121355
NAUTILUS    121 0.005730659026  0.004157729224  0.01492054801
LUX 122 0.002364066194  0.001946639235  0.01090414574
CAMILLE 123 0   0   0.6576280471
AZIR    124 0   0   0.1245590371
SIVIR   125 0   0   0.1135170914
LEBLANC 126 0   0   0.1015179062
RENGAR  127 0   0   0.08380161251
ZIGGS   128 0   0   0.08201257002
TRUNDLE 129 0   0   0.07707617733
WARWICK 130 0   0   0.06314100878
LUCIAN  131 0   0   0.06017393048
TRISTANA    132 0   0   0.05273909414
KATARINA    133 0   0   0.03259969315
ASHE    134 0   0   0.03028659367
\end{filecontents}

\begin{document}
\pgfplotstableread{ntrick.dat}{\champs}
\pgfplotstablegetrowsof{\champs}
\pgfmathsetmacro\totRows{\pgfmathresult}
\begin{tikzpicture}
\sffamily
\begin{axis}[
    xtick=data,% crucial line for the xticklabels directive
    xticklabels from table={\champs}{CHAMP},
    xticklabel style={rotate=90,font=\tiny},
    xmin=-2,
    xmax=137,
    ymin=-0.02,
    ymax=0.6,
    height=480,
    width=820,
    unbounded coords=discard,
    filter discard warning=false,
    cycle list name=color,
]
\draw
    (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) --
    (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);

%Need this so that all xaxis tick labels are printed.
\addplot [
    white,
    only marks, mark=o,
]
table [
    x=X,
    y=Y,
] {\champs};

\foreach \row in {1,...,\totRows} {%
    \addplot+ [
        x filter/.code={\pgfplotstablegetelem{\coordindex}{X}\of{\champs}
                        \ifnum\pgfplotsretval=\row,
                        \else
                        \def\pgfmathresult{}
                        \fi
                       },
        only marks, mark=o,
        error bars/.cd, y dir = both, y explicit
    ]
    table [
        x=X,
        y=Y,
        y error plus expr=\thisrow{YH},
        y error minus expr=\thisrow{YL},
    ] {\champs};
}
\end{axis}

\end{tikzpicture}
\end{document}

结果:

代码结果

相关内容