To rescale a dataset, you can use min-max normalisation, which transforms the values in the dataset to values within a certain range, usually between 0 and 1. Here you see how that works and how to rescale the values to a different range of values.

Rescale a set of data with min-max normalisation in Python.

Here's an app that adjusts the font size according to the screen width in pixels:

As you see, a small window has a small font and a large window has a bigger font.

To calculate the font size from the window width, you can use min-max normalisation.

For this, you specify the folowing values:

Then you can use the normalisation formula to calculate the font sizes for each screen width. Here is an example in Python:

window_widths = [800, 920, 1100, 1400]  # in pixels
min_width = min(window_widths)
max_width = max(window_widths)

min_font_size = 14  # in points
max_font_size = 22  # in points

for w in window_widths:
    normalized = (w - min_width) / (max_width - min_width)  # normalised between 0 and 1
    font_size = min_font_size + normalized * (max_font_size - min_font_size) # rescaled between 14 and 22
    print(f"window width {w} pixels has font size {font_size} points")

Output:

window width 800 pixels has font size 14.0 points
window width 920 pixels has font size 15.6 points
window width 1100 pixels has font size 18.0 points
window width 1400 pixels has font size 22.0 points
Written by Loek van den Ouweland on 2021-08-21.
Questions regarding this artice? You can send them to the address below.