Friday, September 2, 2022

[SOLVED] How can I display matplotlib figure on Sense Hat 8x8 LED Matrix

Issue

I am fetching two variables from a website and taking four instances of each like as I have shown below

Date a   
x 180    
y 16.57
Date b
x 140.17
y 149
Date c
x 254.71
y 200
Date d
x 248.42
y 168

Next I am trying to display the Matplotlib line plot of both variables against time on 8x8 RGB LED matrix. Since there are two dependent variables so I can use colors for both lines in the plot. I can save the matplotlib figure and According to the documentation of the sense hat API I can load the image on by using

sense.load_image("space_invader.png")

It Loads an image file, converts it to RGB format and displays it on the LED matrix. The image must be 8 x 8 pixels in size.

It is bound by pixel size of the image to be loaded, how can I ensure that these bounds are hold by matplotlib? Will it be possible to display the plot on 8x8 figure?

UPDATE: I tried to create the 8x8 plot and setting its DPI to 1

fig, ax = plt.subplots(figsize=(8,8), dpi=1)

But it created an empty figure as shown

plot


Solution

You can make an 8x8 png by:

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(dpi=100, figsize=(8/100, 8/100))
ax = fig.add_axes([0, 0, 1, 1])
ax.plot(np.arange(10), linewidth=0.12)
ax.axis('off')
fig.savefig('Test8x8.png', dpi=100)

you will probably want to change the line width...

enter image description here



Answered By - Jody Klymak
Answer Checked By - Marilyn (WPSolving Volunteer)