我一直在寻找问题的答案,但似乎找不到。x 轴刻度没有显示最小/最大 tikz,我不明白为什么。
如您所见,最大 x-as 值 -120 和 +120 根本没有显示。相反,它停在 -100 和 100。如何修复此问题?
编辑:完整代码以获取工作示例:
\documentclass{standalone}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{filecontents*}{CAR.txt}
days index
-120 -0.046427872
-119 0.094611246
-118 0.01205288
-117 0.259769259
-116 0.217678758
-115 0.420256667
-114 0.759182339
-113 0.809529856
-112 0.607659275
-111 0.935349867
-110 1.336756918
-109 1.027549318
-108 1.175900809
-107 1.194563348
-106 1.571100807
-105 1.69573102
-104 1.731628046
-103 1.962307241
-102 2.240770921
-101 2.200930169
-100 2.101920712
-99 1.813624536
-98 1.966640267
-97 2.553677122
-96 2.407594313
-95 2.744728498
-94 2.482534395
-93 2.567323764
-92 2.089740889
-91 2.349561471
-90 2.045286523
-89 2.272826887
-88 2.245959709
-87 2.161643145
-86 2.314321946
-85 2.18558163
-84 2.281306707
-83 2.312159062
-82 2.186621993
-81 2.619433924
-80 2.481964378
-79 2.937075711
-78 3.209296154
-77 3.408999746
-76 3.190620354
-75 3.347146307
-74 3.552567357
-73 3.581646965
-72 3.590012142
-71 3.96824409
-70 4.200402293
-69 4.153611476
-68 4.371276616
-67 4.47262433
-66 4.648110776
-65 4.45198695
-64 4.605700423
-63 4.613822359
-62 4.820125557
-61 4.958641798
-60 4.429595744
-59 5.074563349
-58 4.97411138
-57 5.452130274
-56 5.531034166
-55 5.661807059
-54 5.797598373
-53 5.605960707
-52 5.626669442
-51 5.661359501
-50 5.903768163
-49 6.100636819
-48 6.577793016
-47 6.482390333
-46 6.737528071
-45 7.09489492
-44 7.171433669
-43 7.453987807
-42 7.293619395
-41 7.37332298
-40 7.218146608
-39 7.099283985
-38 7.180437098
-37 6.998016175
-36 7.192852301
-35 7.226714806
-34 7.026026328
-33 7.734700991
-32 7.534517518
-31 7.694347888
-30 7.494910347
-29 7.671297728
-28 7.682697807
-27 8.18636185
-26 8.604662784
-25 8.486737871
-24 8.753302265
-23 9.184990249
-22 9.570058761
-21 9.68580108
-20 9.801251657
-19 10.16204548
-18 10.20041543
-17 10.20350842
-16 10.60233887
-15 10.44596398
-14 10.6683481
-13 10.74971166
-12 10.70743439
-11 10.68378367
-10 10.86312103
-9 11.1741442
-8 11.36648843
-7 11.78622948
-6 12.14178716
-5 12.14244061
-4 12.44782131
-3 12.55386616
-2 10.49746313
-1 7.804501713
0 7.499873999
1 7.452518286
2 7.380199354
3 7.444947496
4 7.387533278
5 7.503409096
6 7.494028073
7 7.532264001
8 7.659813214
9 7.541537129
10 7.467881268
11 7.209914532
12 7.396632584
13 7.378373011
14 7.49593682
15 7.598044993
16 7.728326093
17 7.861968926
18 7.838373347
19 7.705546725
20 7.59629903
21 7.290861727
22 7.355452591
23 7.649266729
24 7.514485799
25 7.495785251
26 7.410525089
27 7.217119254
28 7.614564875
29 7.213714401
30 7.943953331
31 7.652004873
32 7.661889351
33 7.673483281
34 7.889549818
35 7.886148887
36 8.066205198
37 7.998634224
38 7.758933181
39 7.882325397
40 7.780964955
41 8.18546298
42 8.099405851
43 8.290750565
44 8.213646142
45 8.417244372
46 8.392596203
47 8.171634021
48 8.179716989
49 7.691936086
50 7.312266345
51 7.147885886
52 6.979566243
53 7.20352727
54 7.322889396
55 7.073219466
56 7.334474151
57 7.152291967
58 6.992334604
59 6.653413554
60 6.651287592
61 6.529618586
62 5.599439554
63 5.347442978
64 5.257586584
65 5.14766318
66 6.589825323
67 6.510729019
68 6.559928225
69 6.671527776
70 6.879182703
71 7.010819465
72 6.965074475
73 7.316346602
74 7.493289008
75 7.494192032
76 7.358593724
77 7.384686073
78 7.117525897
79 7.341548559
80 7.176067835
81 6.833253967
82 6.831709728
83 7.082554133
84 7.236915612
85 7.407591304
86 7.31526247
87 7.517171012
88 7.324482084
89 7.087206637
90 6.910408419
91 6.830974654
92 6.601645404
93 6.552802417
94 6.333575125
95 6.072445677
96 6.276207417
97 6.480638178
98 6.553528969
99 6.49722126
100 6.585859322
101 6.746064127
102 6.809323635
103 6.388498889
104 6.288736475
105 6.059787434
106 5.790348143
107 5.743401957
108 5.733415549
109 5.839815012
110 5.59742489
111 5.486038841
112 5.447833365
113 5.64142693
114 5.798187719
115 5.508924137
116 5.649182413
117 5.776923672
118 5.920417455
119 5.992890797
120 5.923469253
\end{filecontents*}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[
xmin=-120,xmax=120,
ymin=0, ymax=15,
xlabel=Event-time (trading days),
ylabel= $\overline{CAR}$ (\%),
xticklabel style={/pgf/number format/1000 sep={}
}
]
\addplot [mark=none,blue] table [x=days,y=index] {CAR.txt};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我建议使用xtick={-120,-80,...,120}
。请注意,xticklabel style={/pgf/number format/1000 sep={}}
鉴于X您正在使用的范围。
\begin{filecontents*}{CAR.txt}
days index
-120 -0.046427872
-119 0.094611246
-118 0.01205288
-117 0.259769259
-116 0.217678758
-115 0.420256667
-114 0.759182339
-113 0.809529856
-112 0.607659275
-111 0.935349867
-110 1.336756918
-109 1.027549318
-108 1.175900809
-107 1.194563348
-106 1.571100807
-105 1.69573102
-104 1.731628046
-103 1.962307241
-102 2.240770921
-101 2.200930169
-100 2.101920712
-99 1.813624536
-98 1.966640267
-97 2.553677122
-96 2.407594313
-95 2.744728498
-94 2.482534395
-93 2.567323764
-92 2.089740889
-91 2.349561471
-90 2.045286523
-89 2.272826887
-88 2.245959709
-87 2.161643145
-86 2.314321946
-85 2.18558163
-84 2.281306707
-83 2.312159062
-82 2.186621993
-81 2.619433924
-80 2.481964378
-79 2.937075711
-78 3.209296154
-77 3.408999746
-76 3.190620354
-75 3.347146307
-74 3.552567357
-73 3.581646965
-72 3.590012142
-71 3.96824409
-70 4.200402293
-69 4.153611476
-68 4.371276616
-67 4.47262433
-66 4.648110776
-65 4.45198695
-64 4.605700423
-63 4.613822359
-62 4.820125557
-61 4.958641798
-60 4.429595744
-59 5.074563349
-58 4.97411138
-57 5.452130274
-56 5.531034166
-55 5.661807059
-54 5.797598373
-53 5.605960707
-52 5.626669442
-51 5.661359501
-50 5.903768163
-49 6.100636819
-48 6.577793016
-47 6.482390333
-46 6.737528071
-45 7.09489492
-44 7.171433669
-43 7.453987807
-42 7.293619395
-41 7.37332298
-40 7.218146608
-39 7.099283985
-38 7.180437098
-37 6.998016175
-36 7.192852301
-35 7.226714806
-34 7.026026328
-33 7.734700991
-32 7.534517518
-31 7.694347888
-30 7.494910347
-29 7.671297728
-28 7.682697807
-27 8.18636185
-26 8.604662784
-25 8.486737871
-24 8.753302265
-23 9.184990249
-22 9.570058761
-21 9.68580108
-20 9.801251657
-19 10.16204548
-18 10.20041543
-17 10.20350842
-16 10.60233887
-15 10.44596398
-14 10.6683481
-13 10.74971166
-12 10.70743439
-11 10.68378367
-10 10.86312103
-9 11.1741442
-8 11.36648843
-7 11.78622948
-6 12.14178716
-5 12.14244061
-4 12.44782131
-3 12.55386616
-2 10.49746313
-1 7.804501713
0 7.499873999
1 7.452518286
2 7.380199354
3 7.444947496
4 7.387533278
5 7.503409096
6 7.494028073
7 7.532264001
8 7.659813214
9 7.541537129
10 7.467881268
11 7.209914532
12 7.396632584
13 7.378373011
14 7.49593682
15 7.598044993
16 7.728326093
17 7.861968926
18 7.838373347
19 7.705546725
20 7.59629903
21 7.290861727
22 7.355452591
23 7.649266729
24 7.514485799
25 7.495785251
26 7.410525089
27 7.217119254
28 7.614564875
29 7.213714401
30 7.943953331
31 7.652004873
32 7.661889351
33 7.673483281
34 7.889549818
35 7.886148887
36 8.066205198
37 7.998634224
38 7.758933181
39 7.882325397
40 7.780964955
41 8.18546298
42 8.099405851
43 8.290750565
44 8.213646142
45 8.417244372
46 8.392596203
47 8.171634021
48 8.179716989
49 7.691936086
50 7.312266345
51 7.147885886
52 6.979566243
53 7.20352727
54 7.322889396
55 7.073219466
56 7.334474151
57 7.152291967
58 6.992334604
59 6.653413554
60 6.651287592
61 6.529618586
62 5.599439554
63 5.347442978
64 5.257586584
65 5.14766318
66 6.589825323
67 6.510729019
68 6.559928225
69 6.671527776
70 6.879182703
71 7.010819465
72 6.965074475
73 7.316346602
74 7.493289008
75 7.494192032
76 7.358593724
77 7.384686073
78 7.117525897
79 7.341548559
80 7.176067835
81 6.833253967
82 6.831709728
83 7.082554133
84 7.236915612
85 7.407591304
86 7.31526247
87 7.517171012
88 7.324482084
89 7.087206637
90 6.910408419
91 6.830974654
92 6.601645404
93 6.552802417
94 6.333575125
95 6.072445677
96 6.276207417
97 6.480638178
98 6.553528969
99 6.49722126
100 6.585859322
101 6.746064127
102 6.809323635
103 6.388498889
104 6.288736475
105 6.059787434
106 5.790348143
107 5.743401957
108 5.733415549
109 5.839815012
110 5.59742489
111 5.486038841
112 5.447833365
113 5.64142693
114 5.798187719
115 5.508924137
116 5.649182413
117 5.776923672
118 5.920417455
119 5.992890797
120 5.923469253
\end{filecontents*}
%
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[
xmin=-120,xmax=120,
ymin=0, ymax=15,
xlabel={Event-time (trading days)},
ylabel={$\overline{CAR}$ (\%)},
xtick={-120,-80,...,120},
xticklabel style={/pgf/number format/1000 sep={}},
]
\addplot [mark=none,blue] table [x=days,y=index] {CAR.txt};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
我想,实现您想要的最简单方法是添加xtick distance=30
(或使用类似的值)选项axis
。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-120,xmax=120,
ymin=0, ymax=15,
xlabel=Event-time (trading days),
ylabel= $\overline{CAR}$ (\%),
xtick distance=30, % <-- added
]
% for simplicity changed your data
\addplot [blue,domain=-120:120] {15*rnd};
\end{axis}
\end{tikzpicture}
\end{document}