使用 pgfplots 绘制对数图

使用 pgfplots 绘制对数图

我刚刚发现我们可以直接用 pgfplots 从现有数据中绘制图表:这太神奇了。出于好奇,我尝试以对数刻度绘制一系列数据,但一直出现我无法理解的错误。我认为这是由于负值造成的,但即使我将其删除,我仍然会收到警告:

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat = newest}

\begin{document}

\begin{tikzpicture}

\begin{axis}[
    xmode = log,
    ymin = 0, ymax = 0.025,
    xtick distance = {1, 10, 100, 1000},
    ytick distance = 0.005,
    grid = both,
    minor tick num = 1,
    major grid style = {lightgray},
    minor grid style = {lightgray!25},
    width = \textwidth,
    height = 0.5\textwidth,
    xlabel = {$t[s]$},
    ylabel = {$R[m]$},
]

% Plot data from a file
\addplot[
    smooth,
    thin,
    red,
    line width = 2
] file[skip first] {data_Q_4e-10.txt};
\legend{Plot from expression}

\end{axis}

\end{tikzpicture}

\end{document}

错误 :

在此处输入图片描述

抱歉,如果这是一个新手问题,但它来自哪里?

谢谢您的帮助 !

真挚地,

数据 :

2   0.0022462518084941
2.2 0.00234057662954396
2.8 0.00245897499012669
3   0.00238307955515855
3.2 0.00243582807687407
3.4 0.00244244669659957
3.6 0.0024827337544152
3.8 0.00246779239251782
4   0.00252528494165521
4.2 0.00253618733858034
4.4 0.00256552968317683
4.6 0.00264384917483666
4.8 0.00268767225621729
5   0.00273562476509036
5.2 0.0027652922480246
5.4 0.00282525106976295
5.6 0.00284306441280999
5.8 0.00288610012494354
6   0.0029054602775201
6.2 0.0029523611287304
6.4 0.002984300855649
6.6 0.00299931148771832
6.8 0.00304206656164785
7   0.0030701117161485
7.2 0.00307555645114942
7.4 0.00309040877893802
7.6 0.00312619837400551
7.8 0.00312676166991218
8   0.00312385021439556
8.2 0.00321342685630374
8.4 0.00324207204850132
8.6 0.00326030890405342
8.8 0.00337192485410837
9   0.00334798850625408
9.2 0.00339880287840911
9.4 0.00341362456261258
9.6 0.00347024929652643
9.8 0.00343479732118626
10  0.00349107865325297
10.2    0.00348122765136823
10.4    0.00349687520957894
10.6    0.00353494391864485
10.8    0.00354663245596456
11  0.00359602773931175
11.2    0.00357630225030555
11.4    0.00359676228045385
11.6    0.00369617353730552
11.8    0.00373739695901366
12  0.00372717322493011
12.2    0.00371463055747229
12.4    0.00372070995945745
12.6    0.00376774591528383
12.8    0.00379243845334319
13  0.00380040195274352
13.2    0.0038020235317798
13.4    0.00382396268405958
13.6    0.00389220625035207
13.8    0.00389024502302335
14  0.0038961257432895
14.2    0.00394583240053267
14.4    0.00398425267911834
14.6    0.00400621962783639
14.8    0.00402813965079758
15  0.00405566257213604
15.2    0.00406173739109111
15.4    0.00409713759978451
15.6    0.00412569440192267
15.8    0.00419518689658554
16  0.00419819437488672
16.2    0.00421556758710133
16.4    0.00426079219369604
16.6    0.00428771169140597
16.8    0.00419861385175248
17  0.00421786469067046
17.2    0.00426182540525236
17.4    0.00428695860249327
17.6    0.00431874713091073
17.8    0.00434233442459172
18  0.00435455202097532
18.2    0.00434848126377709
18.4    0.00438417722189504
18.6    0.00440288341470962
18.8    0.00441739267636023
19  0.00443668660115081
19.2    0.00444455242834544
19.4    0.004474631337817
19.6    0.00446096630343698
19.8    0.00448877791988839
20  0.00449746641211303
20.2    0.00451277736850009
20.4    0.00452751793717911
20.6    0.00455292511982167
20.8    0.00456670063201807
21  0.00461013642477507
21.2    0.00463521740797504
21.4    0.00464685448459271
21.6    0.00466513659994895
21.8    0.00468942274748294
22  0.00473694000725537
22.2    0.00472055302832776
22.4    0.00472223157986161
22.6    0.0047488222929323
22.8    0.00476683649331369
23  0.00475555475382934
23.2    0.0047702219654466
23.4    0.00480675525708315
23.6    0.00482333621016702
23.8    0.00483986035863849
24  0.00485330528224026
25  0.00493837911264291
26  0.00503257979630131
27  0.00519817946358644
28  0.00529729294355089
29  0.00532911344806403
30  0.005325036042304
31  0.00542599797916271
32  0.00553063756834653
33  0.00549587225797363
34  0.00558445612602377
35  0.00567739821632719
36  0.00577647015572303
37  0.00580804064744851
38  0.00587941547680661
39  0.00596619151630793
40  0.00598284625268888
41  0.0060264494478243
42  0.00603948846046822
43  0.00607819622854008
44  0.00619789009999275
45  0.0061627452313547
46  0.00624887483948688
47  0.0062587780869707
48  0.00633540876695504
49  0.00641780444069037
50  0.00638708807255524
51  0.00647231844589021
52  0.0064918350433411
53  0.00658489676520643
54  0.00662865779835202
55  0.00666971180496113
56  0.00670057817064996
57  0.00672707193119861
58  0.00673923453914431
59  0.00686031645781326
60  0.00685213954968048
61  0.00693986419558591
62  0.00694142893779267
63  0.00702569995468046
64  0.00702202249641581
65  0.00706926505125274
66  0.00708253927585678
67  0.00708369958701723
68  0.00714443344803381
69  0.00718490982727062
70  0.00726049804993979
71  0.00733058410996936
72  0.0073505372988632
73  0.00735516796137659
74  0.00742412317606726
75  0.00739841963360028
76  0.00748883876418
77  0.00755684620331174
78  0.00755952581903628
79  0.00766050344437236
80  0.00762709540622308
81  0.00766490855358732
83  0.00776307914797372
85  0.0078251419883626
87  0.00783889612491058
89  0.00796157731134964
91  0.00798554188719255
93  0.00804887980769174
95  0.0080647639485567
97  0.00816236874416992
99  0.00822452343085569
101 0.00826322594387178
103 0.00829704425806261
105 0.0084040410979138
107 0.00842110274960506
109 0.00847211619347948
111 0.00855304309342798
113 0.00852915882452737
115 0.00864788660481058
117 0.00866152034027992
119 0.00877320700197683
121 0.00887019705071806
123 0.00886950211594634
125 0.00889187814178803
127 0.00894033703742113
129 0.00905830923390246
131 0.00905824442638142
133 0.00911879928840596
135 0.00917761150674917
137 0.00919489771694281
139 0.0092890019258503
141 0.00927388535208395
143 0.00930179067638429
146 0.00942710525894446
151 0.00951875995570315
156 0.00960031151560088
161 0.00971933302087975
166 0.0098176795073328
171 0.00995944553071958
176 0.0100162230999568
181 0.0101393084134153
186 0.0102204173684753
191 0.0103289169842988
196 0.0104175404648477
201 0.01057109376821
206 0.0105922584175431
211 0.0107727269731553
216 0.010790395650611
221 0.010927816449913
226 0.0109669353166326
236 0.0111518696226161
246 0.011367042759839
256 0.0115557601124109
266 0.0115744144326293
276 0.0117534879027411
286 0.0118710976467428
296 0.01201472626401
306 0.0121598447740596
316 0.0123372015069756
326 0.0124542954802421
376 0.0130911960221545
426 0.0136475101399497
476 0.0141368484061226
526 0.0146405727982913
576 0.0150462288312424
626 0.0154747270948655
676 0.0158995420129521
726 0.0162819063772665
776 0.0166176503740592
826 0.0169922870947506
876 0.0173192617482491
926 0.0176438369999518
976 0.0180128564849312
1026    0.0183481695344701
1076    0.0187160207574586
1126    0.0190542874114053
1176    0.0193740972604341
1226    0.0197260672227984
1276    0.020127665249735

目标结果是 MATLAB 风格:

在此处输入图片描述

答案1

问题与轴设置有关,禁用时也可以看到\addplot。如果没有这两行注释,它就可以正常运行:

\begin{axis}[
    xmode = log,
%    ymin = 0, ymax = 0.025,
%    xtick distance = {1, 10, 100, 1000},
    ytick distance = 0.005,
    grid = both,
    minor tick num = 1,
    major grid style = {lightgray},
    minor grid style = {lightgray!25},
    width = \textwidth,
    height = 0.5\textwidth,
    xlabel = {$t[s]$},
    ylabel = {$R[m]$},
]

结果

跟进:pgfplots 提供不同的轴,例如loglogaxis

日志日志

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat = newest}

\begin{document}

\begin{tikzpicture}

\begin{loglogaxis}[
%    xmode = log,
%    ymin = 0, ymax = 0.025,
%    xtick distance = {1, 10, 100, 1000},
    ytick distance = 0.005,
    grid = both,
    minor tick num = 1,
    major grid style = {lightgray},
    minor grid style = {lightgray!25},
    width = \textwidth,
    height = 0.5\textwidth,
    xlabel = {$t[s]$},
    ylabel = {$R[m]$},
]

% Plot data from a file
\addplot[
    smooth,
    thin,
    red,
    line width = 2
] file[skip first] {data_Q_4e-10.txt};
\legend{Plot from expression}

\end{loglogaxis}

\end{tikzpicture}

\end{document}

相关内容