Python 与 org-mode babel 输出图形中的 R
Python vs. R in org-mode babel output graphics
我正在尝试在组织模式下编写报告。
从csv文件中读取数据(单列三行,浮点数),在条形图中进行比较,将图表嵌入到报告中,以便可以将其导出为latex,然后导出为pdf。
我很难理解我在 python 代码的条形图创建部分所做的事情,因为 R_plot 工作正常,这意味着图表在相同的组织模式下嵌入到报告中:导出:结果:文件设置。
python 代码我做错了什么?如果我在交互模式下 运行 python 代码,它可以毫无问题地生成图表,但由于某些原因 py_comparison.png 当我 运行 按单元格块时未保存。
#+NAME: R_plot
#+BEGIN_SRC R :exports both :results output graphics :file r_comparison.png
# graph in R
library("ggplot2")
performance <- read.csv("comparison.csv", header=FALSE)$V1
df <- data.frame(resource = c("1node1core", "1node8core", "2node8core"), performance = performance)
p <- ggplot(data = df, aes(x=resource, y=performance)) +
geom_bar(stat="identity", fill="steelblue") +
theme_minimal() +
ggtitle("Computation time (min) vs. Resource (type)")
p
#+END_SRC
#+NAME: python_plot
#+BEGIN_SRC python :exports both :results output graphics :file py_comparison.png
import matplotlib.pyplot as plt; plt.rcdefaults()
import matplotlib.pyplot as plt
import csv
objects = ['1node1core', '1node8core', '2node8core']
y_pos = list(range(0, len(objects)))
performance = []
with open('comparison.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
f_row = float(row[0])
performance.append(f_row)
plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('Time')
plt.title('Resource vs. Time')
plt.show()
#+END_SRC
+END_SRC
我认为 python 代码块的 header 值是错误的。 :results <file name> file
和:file <file name>
是有区别的。根据文档(为清楚起见进行了编辑):
There are four classes of :results header arguments. Each ‘src’ code
block can take only one option per class. [...]
Collection [...]
- value Default. Functional mode. Result is the value returned by the last statement in the ‘src’ code block. Languages like Python may
require an explicit return statement in the ‘src’ code block. Usage
example: :results value.
Type [...]
- file Interpret as path to a file. Inserts a link to the file. Usage example: :results value file.
An external :file that saves the results of execution of the code
block. [...] Some languages, such as ‘R’, ‘dot’, ‘ditaa’, and ‘gnuplot’,
automatically wrap the source code in additional boilerplate code.
Such code wrapping helps recreate the output, especially graphics
output, by executing just the :file contents.
在 python 中,plt.show()
(或 savefig
就此而言)的结果是 None
,图像只是一个副作用,因此不会插入任何内容。在 R 中,由于上面提到的样板包装器,它可以工作
因此,在 python 中,您需要保存并 return 您的图像,而不是显示它。这样的事情应该有效:
#+NAME: python_plot
#+BEGIN_SRC python :results img.png file
import matplotlib.pyplot as plt
plt.plot(range(5))
plt.savefig('img.png')
return 'img.png'
#+END_SRC
我正在尝试在组织模式下编写报告。 从csv文件中读取数据(单列三行,浮点数),在条形图中进行比较,将图表嵌入到报告中,以便可以将其导出为latex,然后导出为pdf。
我很难理解我在 python 代码的条形图创建部分所做的事情,因为 R_plot 工作正常,这意味着图表在相同的组织模式下嵌入到报告中:导出:结果:文件设置。
python 代码我做错了什么?如果我在交互模式下 运行 python 代码,它可以毫无问题地生成图表,但由于某些原因 py_comparison.png 当我 运行 按单元格块时未保存。
#+NAME: R_plot
#+BEGIN_SRC R :exports both :results output graphics :file r_comparison.png
# graph in R
library("ggplot2")
performance <- read.csv("comparison.csv", header=FALSE)$V1
df <- data.frame(resource = c("1node1core", "1node8core", "2node8core"), performance = performance)
p <- ggplot(data = df, aes(x=resource, y=performance)) +
geom_bar(stat="identity", fill="steelblue") +
theme_minimal() +
ggtitle("Computation time (min) vs. Resource (type)")
p
#+END_SRC
#+NAME: python_plot
#+BEGIN_SRC python :exports both :results output graphics :file py_comparison.png
import matplotlib.pyplot as plt; plt.rcdefaults()
import matplotlib.pyplot as plt
import csv
objects = ['1node1core', '1node8core', '2node8core']
y_pos = list(range(0, len(objects)))
performance = []
with open('comparison.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
f_row = float(row[0])
performance.append(f_row)
plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('Time')
plt.title('Resource vs. Time')
plt.show()
#+END_SRC
+END_SRC
我认为 python 代码块的 header 值是错误的。 :results <file name> file
和:file <file name>
是有区别的。根据文档(为清楚起见进行了编辑):
There are four classes of :results header arguments. Each ‘src’ code block can take only one option per class. [...]
Collection [...]
- value Default. Functional mode. Result is the value returned by the last statement in the ‘src’ code block. Languages like Python may require an explicit return statement in the ‘src’ code block. Usage example: :results value.
Type [...]
- file Interpret as path to a file. Inserts a link to the file. Usage example: :results value file.
An external :file that saves the results of execution of the code block. [...] Some languages, such as ‘R’, ‘dot’, ‘ditaa’, and ‘gnuplot’, automatically wrap the source code in additional boilerplate code. Such code wrapping helps recreate the output, especially graphics output, by executing just the :file contents.
在 python 中,plt.show()
(或 savefig
就此而言)的结果是 None
,图像只是一个副作用,因此不会插入任何内容。在 R 中,由于上面提到的样板包装器,它可以工作
因此,在 python 中,您需要保存并 return 您的图像,而不是显示它。这样的事情应该有效:
#+NAME: python_plot
#+BEGIN_SRC python :results img.png file
import matplotlib.pyplot as plt
plt.plot(range(5))
plt.savefig('img.png')
return 'img.png'
#+END_SRC