我试图将使用rpy2
创建的ggplot2
R对象的输出保存到Python控制的缓冲区中。在
我可以使用matplotlib
来实现这一点,但我似乎无法通过rpy2
使用ggplot2来实现。在
在matplotlib.pyplot
中,这可以通过以下方式实现:
import matplotlib.pyplot as plt
import io
import numpy
def test_save():
x = numpy.linspace(-5, 5)
y = 3*x + 2
fig = plt.figure()
plt.plot(x, f)
buf = io.BytesIO()
plt.savefig(buf, format = 'png')
return buf
ggplot2
尝试:
错误:
NotImplementedError: Conversion 'py2ri' not defined for objects of type ''
我试图使用^{
R的
ggplot2::ggsave
需要一个字符串,指定路径(相对或绝对)作为参数filename
的参数。例如"/this/is/my/figure.png"
。在Python
BytesIO
对象是完全不同的。它是一个内存中的二进制流(大致是Python对象,其行为类似于(二进制)文件)。在如果使用
ggsave
不是绝对要求,请考虑使用rpy2.robjects.lib.grdevices.render_to_bytesio()
。这是一个在Jupyter笔记本中显示内联数字的函数:相关问题 更多 >
编程相关推荐