How to display python matplotlib graphs (png) with Chaquopy in Android Studio

假装没事ソ 提交于 2019-12-11 03:15:38

问题


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.

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() 

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.

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

Thanks for any advice


回答1:


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

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()

... and then load that bytes object into the ImageView like in this app.



来源:https://stackoverflow.com/questions/53285429/how-to-display-python-matplotlib-graphs-png-with-chaquopy-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!