-
Notifications
You must be signed in to change notification settings - Fork 0
/
20240407101327-emacs_调用matplotlib画图_并存储到windows的临时文件夹中.html
498 lines (442 loc) · 17.2 KB
/
20240407101327-emacs_调用matplotlib画图_并存储到windows的临时文件夹中.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2025-01-02 周四 15:00 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>+emacs+调用matplotlib画图,并存储到Windows的临时文件夹中</title>
<meta name="generator" content="Org Mode" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
/*@import url("https://fonts.googleapis.com/css2?family=Lora:wght@500&display=fallback");
@import url("https://fonts.googleapis.com/css2?family=Inconsolata&display=fallback");
*/
body {
margin: 2% auto;
width: 90%;
max-width: 100%;
background-color: rgba(242,229,188,0.5);
/* background-color: rgba(255,255,255,0.5); */
font-size: 18px;
line-height: 1.4em;
}
@media screen and (min-width: 600px) {
body {
font-size: 18px;
}
}
@media screen and (min-width: 910px) {
body {
width: 900px;
}
}
#preamble {
width: 80%;
max-width: 980px;
text-align: right;
margin: auto;
}
hr {
border: none;
border-top: 5px double #333;
margin: 0px 0;
}
.button {
border: none;
color: black;
padding: 6px 14px;
text-align: center;
text-decoration: none;
display: inline-block;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
}
.button:hover {
background-color: #3a1616;
color: white;
}
.todo {
background-color: #9d0006;
color: #fbf1c7;
padding: .1em 0.3em;
border-radius: 3px;
background-clip: padding-box;
font-size: $code-size;
font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace;
line-height: 1;
}
.done {
background-color: #427b58;
color: #fbf1c7;
padding: .1em 0.3em;
border-radius: 3px;
background-clip: padding-box;
font-size: $code-size;
font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace;
line-height: 1;
}
#postamble p {
font-size: 80%;
margin: .2em;
}
#content, .content {
width: 80%;
max-width: 980px;
margin: auto;
}
#org-div-home-and-up{
position: fixed;
right: 0.5em;
margin-top: 70px;
font-family:sans-serif;
}
#table-of-contents {
margin-top: 105px;
font-size: 12pt;
position: fixed;
left: 0em;
top: 0em;
background: white;
max-height: 80%;
overflow: auto;
}
#table-of-contents h2 {
display: none;
}
#table-of-contents li {
clear: both;
}
.timestamp {
color: #928374;
font-size: $smaller;
}
.timestamp-kwd {
color: #5f9ea0;
}
@media screen and (max-width: 1200px) {
#table-of-contents {
position: relative;
margin-top: 0px;
}
}
/* p:not(blockquote > p) :not(ol > p) { */
/* text-indent: 2em; */
/* } */
p {
margin: 1em auto;
}
.title {
font-size: 30px;
line-height: 1.3;
font-weight: bold;
color: #0c2340;
}
h2:not(#table-of-contents > h2) {
font-size: 30px;
line-height: 1.2;
position: relative;
color: #204060;
}
h2::after {
content: "";
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 2px;
background-color: black;
}
h3:not(#table-of-contents > h3) {
font-size: 20px;
line-height: 1.1;
position: relative;
color: #4b748d;
}
h3::after {
content: "";
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 2px;
/* border-bottom: 1px dashed black; */
}
h4 {
font-size: 14px;
line-height: 1.1;
font-weight: bold;
}
a {
cursor: pointer;
text-decoration: none;
}
a:link {
color: #458588;
/* text-decoration: underline; */
}
a:hover {
/* font-weight: bold; */
text-decoration: underline;
/* color: #076678; */
}
.figure {
text-align: center;
}
a:visited{
color: #8f3f71;
/* text-decoration: underline; */
}
img {
max-width: 100%;
vertical-align: middle;
}
.MathJax_Display {
margin: 0!important;
width: $smaller!important;
}
@media screen and (max-width: 1200px) {
img {
width: 400px;
}
.MathJax_Display {
margin: 0!important;
width: $smaller!important;
}
}
.org-src-container {
font-size: 14px;
outline: 2px dashed #ccc;
outline-offset: 10px;
position: relative;
}
.org-src-container>pre {
overflow: auto;
font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Cascadia Code,Consolas, Liberation Mono, monospace;
}
.org-src-container>pre:before {
display: block;
position: absolute;
background-color: #b3b3b3;
top: 0;
right: 0;
padding: 0 0.5em;
border-bottom-left-radius: 8px;
border: 0;
color: white;
font-size: $code-size;
}
pre {
overflow: auto;
}
.example{
font-size: 14px;
outline: 2px dashed #ccc;
outline-offset: 10px;
position: relative;
}
.example::before {
content: "Results";
}
pre:before {
display: block;
position: absolute;
background-color: #b3b3b3;
top: 0;
right: 0;
padding: 0 0.5em;
border-bottom-left-radius: 8px;
border: 0;
color: white;
font-size: $code-size;
}
/* .org-src-container::before { */
/* content: "Copy"; */
/* position: absolute; */
/* top: 5px; */
/* right: 5px; */
/* padding: 5px; */
/* background-color: #ccc; */
/* color: #fff; */
/* border-radius: 3px; */
/* cursor: pointer; */
/* } */
code {
font-size: 16px;
color: #427b58;
}
.subtitle {
text-align: right;
}
blockquote {
border-left: 3px solid #ccc;
padding-left: 10px; /* Optional: add some left padding for the content */
margin-inline-start: 0px;
margin-inline-end: 0px;
}
.info {
overflow: hidden;
}
.created {
float: right;
}
.updated {
float: left;
}
.org-ol {
list-style-type: decimal; /* Set the list style type to decimal numbers */
/* margin-left: 0.5em; /\* Add left margin to the list *\/ */
}
b {
color: #9d0006;
}
#footnotes {
position: fixed;
left: auto;
right: 0;
top: 105px;
bottom: 0;
width: 20%;
padding: 10px;
overflow: auto;
font-size: 14px;
z-index: 9999;
}
#footnotes h2 {
display: none;
}
@media screen and (max-width: 1200px) {
#footnotes {
position: static;
left: 0;
right: 0;
bottom: 0;
width: 100%;
max-height: 30%;
overflow: auto;
padding: 10px;
font-size: 14px;
z-index: 9999;
}
}
/*]]>*/-->
</style>
</head>
<body>
<div id="org-div-home-and-up">
<a accesskey="h" href=""> UP </a>
|
<a accesskey="H" href="index.html"> HOME </a>
</div><div id="content" class="content">
<h1 class="title">+emacs+调用matplotlib画图,并存储到Windows的临时文件夹中</h1>
<p>
<span class="timestamp-wrapper"><span class="timestamp">[2024-04-07 周日 10:13]</span></span><br />
</p>
<p>
根据已有表格绘图,在表格上方标注名称,如:<br />
</p>
<div class="org-src-container">
<pre class="src src-org"><span style="color: #a89984;">#+NAME: myTimeTable</span>
<span style="color: #076678; background-color: #ebdbb2;">|1|2|</span>
<span style="color: #076678; background-color: #ebdbb2;">|-+-|</span>
<span style="color: #076678; background-color: #ebdbb2;">|3|4|</span>
</pre>
</div>
<p>
在python代码行的header里传入表格数据(在我用的org 9.6.14中数据会自动去除标题行和分割行,只剩下面的数据行):<br />
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a89984;">#</span><span style="color: #a89984;">+begin_src python :results file link :var data=myTimeTable</span>
<span style="color: #9d0006;">import</span> os,matplotlib, numpy
……
<span style="color: #076678;">savedir</span> = os.getenv<span style="color: #458588;">(</span><span style="color: #79740e;">'TEMP'</span><span style="color: #458588;">)</span>+<span style="color: #79740e;">'</span><span style="color: #8f3f71;">\\</span><span style="color: #79740e;">myTimeTable.png'</span>
plt.savefig<span style="color: #458588;">(</span>savedir<span style="color: #458588;">)</span>
<span style="color: #9d0006;">return</span> savedir
</pre>
</div>
<p>
会把代码块上方的name动态传入到代码中<br />
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a89984;">#</span><span style="color: #a89984;">+name: 代码块name</span>
<span style="color: #a89984;">#</span><span style="color: #a89984;">+begin_src python :var filename=(org-element-property :name (org-element-context)) :results file link</span>
<span style="color: #9d0006;">import</span> os,matplotlib, numpy
……
<span style="color: #076678;">savedir</span> = os.getenv<span style="color: #458588;">(</span><span style="color: #79740e;">'TEMP'</span><span style="color: #458588;">)</span>+<span style="color: #79740e;">'</span><span style="color: #8f3f71;">\\</span><span style="color: #79740e;">'</span>+filename+<span style="color: #79740e;">'.png'</span>
plt.savefig<span style="color: #458588;">(</span>savedir<span style="color: #458588;">)</span>
<span style="color: #9d0006;">return</span> savedir
</pre>
</div>
<p>
运行后会在下方生成<br />
</p>
<div class="org-src-container">
<pre class="src src-org"><span style="color: #a89984;">#+RESULTS:</span>
<span style="color: #689d6a; text-decoration: underline;"><a href="file:..../XXX.png">file:..../XXX.png</a></span>
</pre>
</div>
<p>
的图片,可以配合inline 图片相关函数使用<br />
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #a89984;">;; </span><span style="color: #a89984;">(setq org-startup-with-inline-images t)</span>
<span style="color: #a89984;">;;; </span><span style="color: #a89984;">Only display inline images under current subtree.</span>
<span style="color: #458588;">(</span><span style="color: #9d0006;">defun</span> <span style="color: #b57614;">org-display-subtree-inline-images</span> <span style="color: #b16286;">(</span><span style="color: #8f3f71;">&optional</span> state<span style="color: #b16286;">)</span>
<span style="color: #79740e;">"Toggle the display of inline images under current subtree.</span>
<span style="color: #79740e;">INCLUDE-LINKED is passed to `</span><span style="color: #8f3f71;">org-display-inline-images</span><span style="color: #79740e;">'."</span>
<span style="color: #b16286;">(</span><span style="color: #9d0006;">interactive</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #9d0006;">save-excursion</span>
<span style="color: #8ec07c;">(</span><span style="color: #9d0006;">save-restriction</span>
<span style="color: #d65d0e;">(</span>org-narrow-to-subtree<span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span><span style="color: #9d0006;">let*</span> <span style="color: #458588;">(</span><span style="color: #b16286;">(</span>beg <span style="color: #8ec07c;">(</span>point-min<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>end <span style="color: #8ec07c;">(</span>point-max<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>image-overlays <span style="color: #8ec07c;">(</span>cl-intersection
org-inline-image-overlays
<span style="color: #d65d0e;">(</span>overlays-in beg end<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>display-inline-images-local
<span style="color: #8ec07c;">(</span><span style="color: #9d0006;">lambda</span> <span style="color: #d65d0e;">()</span>
<span style="color: #d65d0e;">(</span>org-display-inline-images t t beg end<span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span><span style="color: #9d0006;">setq</span> image-overlays <span style="color: #458588;">(</span>cl-intersection
org-inline-image-overlays
<span style="color: #458588;">(</span>overlays-in beg end<span style="color: #458588;">)</span><span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span><span style="color: #9d0006;">if</span> <span style="color: #458588;">(</span><span style="color: #9d0006;">and</span> <span style="color: #458588;">(</span>org-called-interactively-p<span style="color: #458588;">)</span> image-overlays<span style="color: #458588;">)</span>
<span style="color: #458588;">(</span>message <span style="color: #79740e;">"%d images displayed inline"</span>
<span style="color: #458588;">(</span>length image-overlays<span style="color: #458588;">)</span><span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span>hide-inline-images-local
<span style="color: #8ec07c;">(</span><span style="color: #9d0006;">lambda</span> <span style="color: #d65d0e;">()</span>
<span style="color: #d65d0e;">(</span>org-remove-inline-images<span style="color: #d65d0e;">)</span>
<span style="color: #d65d0e;">(</span>message <span style="color: #79740e;">"Inline image display turned off"</span><span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span><span style="color: #9d0006;">if</span> state
<span style="color: #b16286;">(</span><span style="color: #9d0006;">pcase</span> state
<span style="color: #8ec07c;">(</span>'subtree
<span style="color: #d65d0e;">(</span>funcall display-inline-images-local<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span>
<span style="color: #8ec07c;">(</span>'folded
<span style="color: #d65d0e;">(</span>funcall hide-inline-images-local<span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span>
<span style="color: #b16286;">(</span><span style="color: #9d0006;">if</span> image-overlays
<span style="color: #8ec07c;">(</span>funcall display-inline-images-local<span style="color: #8ec07c;">)</span>
<span style="color: #8ec07c;">(</span>funcall hide-inline-images-local<span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span><span style="color: #d65d0e;">)</span><span style="color: #8ec07c;">)</span><span style="color: #b16286;">)</span><span style="color: #458588;">)</span>
<span style="color: #458588;">(</span>define-key org-mode-map <span style="color: #b16286;">(</span>kbd <span style="color: #79740e;">"C-c C-x C-v"</span><span style="color: #b16286;">)</span> 'org-display-subtree-inline-images<span style="color: #458588;">)</span>
<span style="color: #a89984;">;;; </span><span style="color: #a89984;">auto display inline images on Org TAB cycle expand headlines.</span>
<span style="color: #458588;">(</span>add-hook 'org-cycle-hook #'org-display-subtree-inline-images<span style="color: #458588;">)</span>
<span style="color: #458588;">(</span>add-hook 'org-babel-after-execute-hook 'org-redisplay-inline-images<span style="color: #458588;">)</span>
</pre>
</div>
<p>
参考:<a href="https://emacs-china.org/t/org-link/13502/6">https://emacs-china.org/t/org-link/13502/6</a><br />
</p>
<div id="outline-container-orgc1857e0" class="outline-2">
<h2 id="orgc1857e0"><span class="section-number-2">1.</span> 通过代码块引用的方式重复使用部分代码</h2>
<div class="outline-text-2" id="text-1">
<p>
在代码块上方加上 <code>#+NAME: line-plot</code> ,然后在需要引用的代码块上加上 <code>:noweb yes</code> 参数,再在内部通过 <code><<pie-plot>></code> 的方式即可复用代码。<br />
</p>
</div>
</div>
</div>
<div id="postamble" class="status">
<p>▲ 编辑于 <span class="timestamp-wrapper"><span class="timestamp">[2024-12-31 周二 15:26]</span></span> | © Published by <a href="https://www.gnu.org/software/emacs/">Emacs</a> 31.0.50 (<a href="https://orgmode.org">Org</a> mode 9.7.18) on <span class="timestamp-wrapper"><span class="timestamp">[2025-01-02 周四 15:00]</span></span> | <a href="https://tomoemami.github.io/rss.xml">RSS</a></p>
</div>
</body>
</html>