如何在Android Studio中使用Chaquopy显示python matplotlib图(png) [英] How to display python matplotlib graphs (png) with Chaquopy in Android Studio

查看:585
本文介绍了如何在Android Studio中使用Chaquopy显示python matplotlib图(png)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我使用chaquopy来使简单的python程序在旧的(软糖)平板电脑中运行(我替换了src目录中示例控制台应用程序的main.py).对于初学者来说还不错,我很高兴.

So I use chaquopy to get simple python programs functioning in an old (jelly bean) tablet (I replace the example console app's main.py in the src directory). Not bad for a beginner's start and I'm very happy.

但是现在为了测试,我尝试显示如下所示的matplotlib图:

But now for a test I try to display a matplotlib graph like this:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def main():

    image = mpimg.imread("/storage/emulated/0/Documents/test.png")
    plt.imshow(image)
    plt.show() 

matplotlib库是从android studio内部安装的(尽管缺少一些元素,手动安装了pip3并使用了本地文件).现在,构建和程序完成,没有错误,但是没有图形.安装枕头以及使用其他图形类型均无济于事.

The matplotlib library was installed from within android studio (albeit with a few missing elements, manual pip3 install and usage of local files). Now the build and program finish without errors, but there is no graph. Installation of pillow and use of other graph types no avail.

这可以用python处理,还是需要深入研究android studio/java?

Can this be handled in python, or is a dive into android studio / java required?

谢谢您的建议

推荐答案

您必须在应用的布局中包含ImageView,然后将图像文件加载到其中,如

You'll have to include an ImageView in your app's layout, and then load the image file into it, as in this answer.

对于由matplotlib动态生成的图像,可以将其保存到文件中,然后从该文件中加载,或者将其保存到字节对象中,如下所示:

For an image which is generated dynamically by matplotlib, either save it to a file and then load from that file, or save it to a bytes object like this:

import io
bio = io.BytesIO()
plt.savefig(bio, format="png")
b = bio.getvalue()

...,然后像 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆