Search code examples
androidmatplotlibkotlindata-visualizationchaquopy

Python Matplotlib on Android


I have a python script that uses matplotlib's plt.show() method to plot graphs. Problem is, my program does not display graphs. What is an easy approach to displaying matplotlib's graphs on say layout xml file? Is this even possible?

I use Kotlin on Android Studio and it calls my Python scripts using Chaquopy btw.


Solution

  • I've answered your question at https://github.com/chaquo/chaquopy/issues/294:

    The easiest way is to save the image in PNG format, then load it using Android's BitmapFactory. You can do this either:

    • Via a temporary file; or
    • Via a Python bytes object which you convert to a Java byte[] array using PyObject.toJava. If matplotlib doesn't have an API to save a PNG directly to a bytes object, then you can use BytesIO.