Python Forum
matplotlib chart text font misery with 5x8 dot matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matplotlib chart text font misery with 5x8 dot matrix
#1
I can't get matplotlib to create my plots the way it needs to. This problem of font representation doesn't exist in Pillow.
After 40 hours of searching, reading, trying with the help of Google and ChatGPT, I give up and turn to you for help.

System description:
I have various sensors that periodically transmit data via radio to a server running Node-Red. Node-Red has various tasks, the only important thing here is that it writes various measurement data into an SQLite database.
There is an information display in the living room that shows, among other things, various diagrams. It consists of a 7.5 inch e-paper display from Waveshare with a resolution of 800x480 pixels and a color depth of 1 bit (black and white). It is controlled by an ESP32 microcontroller. It wakes up every quarter of an hour, gets the pixel data (48 Kbytes) from the server via web socket, pushes it onto the display and goes back to sleep.
A Python program runs on the server, which extracts and transmits the pixel data for the display from a bitmap file upon request from the ESP32.
A Python program also runs on the server, which, triggered by Node-Red, fetches data from the database, creates various diagrams, arranges them on a canvas of 800x480 pixels and then saves this canvas as a bitmap file with 1 bit color depth.
All of these components are currently working together.

The problem lies in the presentation of the matplotlib diagrams. Because the display has such a low resolution, the text to be displayed must be correspondingly small and still be reasonably readable. The choosen font is a dot matrix structure of 8x5 pixels (5x8_lcd_hd44780u_a02.ttf). And this way, text characters must be max 8 pixel high and 5 pixel wide.

I use Python 3.12.1, matplotlib 3.8.4 and pillow 10.3.0 on a Windows 10 PC.
The font is installed and included in the fontlist-v330.json file.

I'm attaching an example code to play with and the resulting bitmap. The way the text looks in pillow figures is how it should look in the matplotlib charts. Also attached is a bitmap from my actual regular code under development, just to get a rough idea.


import matplotlib.pyplot as plt
from matplotlib import font_manager as fm, rcParams
from PIL import Image, ImageDraw, ImageFont

fig, ax = plt.subplots(figsize=(2.64, 2.32), dpi=100)
custom_font = "5x8 LCD HD44780U A02"

ax.set_title('Test-Txt', fontname = custom_font, size = 8)
ax.set_xlabel('xLabel', fontname = custom_font, size = 8)
ax.set_ylabel('yLabel', fontname = custom_font, size = 8)
plt.text(.1, .5, 'TXT pos .2.5', fontname = custom_font, size = 8)

plt.tick_params(axis='both', which='both', labelsize=8)
for tick in ax.get_xticklabels():
    tick.set_fontname(custom_font)

for tick in ax.get_yticklabels():
    tick.set_fontname(custom_font)
    
plt.savefig('tst.png')

chart = Image.open('tst.png')
canvas = Image.new("1", (800, 480), "white")
draw = ImageDraw.Draw(canvas)

text = 'ImageFontTest'
font_name = "5x8.ttf"
font_size = 8
font = ImageFont.truetype(font_name, font_size)

canvas.paste(chart, (50, 50))
draw.text((98, 123), 'PilDrawText_098/123', font=font, fill="black")

canvas.save("result.bmp")

Attached Files

.bmp   example-result.bmp (Size: 46.94 KB / Downloads: 2)
.bmp   regular-canvas.bmp (Size: 46.94 KB / Downloads: 2)
Reply
#2
The fonts they use on books like Python books, they use fortran programing the fonts get smaller. This is done on windows, so the 8 fonts is the smallest.
Programs are like instructions or rules. Learning it gets us closer to a solution. Desired outcome. Computer talk.
Reply
#3
What does it look like when you use a standard font?
Reply
#4
(May-23-2024, 03:29 AM)deanhystad Wrote: What does it look like when you use a standard font?

Attached Code and Bitmap using default mpl font and 8x5 font with pil.


import matplotlib.pyplot as plt
from matplotlib import font_manager as fm, rcParams
from PIL import Image, ImageDraw, ImageFont

fig, ax = plt.subplots(figsize=(2.64, 2.32), dpi=100)
plt.tick_params(axis='both', which='both', labelsize=8)

ax.set_title('mpl_TEXT-aBCDEFklmnOPQstwXYZ/0123456789', size = 8)
ax.set_xlabel('xLabel', size = 8)
ax.set_ylabel('yLabel', size = 8)
plt.text(.1, .5, 'TXT pos .2.5', size = 8)
    
plt.savefig('tst.png')

chart = Image.open('tst.png')
canvas = Image.new("1", (800, 480), "white")
draw = ImageDraw.Draw(canvas)

text = 'ImageFontTest'
font_name = "5x8.ttf"
font_size = 8
font = ImageFont.truetype(font_name, font_size)

canvas.paste(chart, (10, 50))
draw.text((12, 50), 'pil_TEXT-aBCDEFklmnOPQstwXYZ/0123456789', font=font, fill="black")

canvas.save("result.bmp")

Attached Files

.bmp   result.bmp (Size: 46.94 KB / Downloads: 2)
Reply
#5
Have you tried using LaTeX to render the font?
https://matplotlib.org/stable/users/expl...setex.html
Reply
#6
Tahks for the advise.
No, I did not. But I will give it a try on weekend.
Reply
#7
This will look comparably small but the graph will also be smaller in comparison. Will it turn out like a book? Python book for example on the footnotes or endnotes. Which is the idea behind it. Using the Python programs then and not the Microsoft version. This is not bad. Though I don't use Microsoft word too often, it's only because of the Java thing, you know. But the idea of using ASP.net and learning their way gets me too up tight. So I'd rather learn it another way if possible. Python or Java will do.
Programs are like instructions or rules. Learning it gets us closer to a solution. Desired outcome. Computer talk.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Visualizing musician chart performance and ranking using pandas and matplotlib Drone4four 5 762 May-12-2024, 02:23 AM
Last Post: Drone4four
Question I’m trying to create a Power BI Matplotlib quadrant chart and I’m a little stumped. Nidwolff 1 610 Mar-04-2024, 06:07 AM
Last Post: Danishhafeez
  Rename labels of a bar chart Matplotlib smalatray 1 4,480 Jul-01-2020, 01:48 AM
Last Post: hussainmujtaba
  Matplotlib bar chart ollarch 0 1,496 Mar-04-2020, 10:45 AM
Last Post: ollarch
  Spacing pie chart colours evenly in matplotlib? Giovanni_diJacopo 1 3,442 Jul-12-2019, 12:31 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020