Marsilea-viz / marsilea

Declarative creation of composable visualization for Python (Complex heatmap, Upset plot, Oncoprint and more~)
https://marsilea.rtfd.io/
MIT License
166 stars 6 forks source link

Free Font Awesome icons not rendering #23

Closed aadimator closed 7 months ago

aadimator commented 7 months ago

I've trying to render the Font Awesome Icons using mpl_fontkit and the examples provided on the website. The icons in the example work fine, but when I change the utf8 to some other "Free" icon, it doesn't show. It just shows a blank box. I've tried several icons, all free, and they don't work. I've tried a couple of "Brands" icons as well, and they seem to be working fine. I have no idea what the problem could be.

Here's a basic program for reproducible problem:

import marsilea as ma
import marsilea.plotter as mp
import mpl_fontkit as fk
import numpy as np

fk.install_fontawesome(verbose=False)

data = np.random.randint(0, 3, 10)

# mapper = {0: '\uf58a', 1: '\uf11a', 2: '\uf567'}
mapper = {0: '\uf183', 1: '\uf182', 2: '\uf221'}
cmapper = {0: '#609966', 1: '#DC8449', 2: '#F16767'}
flavour = [mapper[i] for i in data]
flavour_colors = [cmapper[i] for i in data]
labels = mp.Labels(flavour, fontfamily='Font Awesome 6 Free',
              text_props={'color': flavour_colors})
_, ax = plt.subplots()
labels.render(ax)

And the output is: image

When I uncomment the original icons as specified in the Oil example, they seem to be working fine: image

But changing the utf8 values produces the above output. Can you tell me what I'm doing wrong here?

Mr-Milk commented 7 months ago

You need to add fontweight=900 to display it, notice that it must be this exact number.

import mpl_fontkit as fk
import matplotlib.pyplot as plt

fk.install_fontawesome(verbose=False)

plt.text(.5, .5, "\uf183\uf182\uf221", fontfamily="Font Awesome 6 Free", fontweight=900)

And you get: image

In case you want to know, Font awesome distributed the "Free" font in two styles, weight 400 and weight 900. Unfortunately, it's not way for us to tell which icon is available in which style. So if you find something doesn't work, try either fontweight=400 or 900. However, if one of your glyph is in 400 and one is in 900, and you want to plot them together, it won't be easy. But that's what we can get for a free version.

aadimator commented 7 months ago

Thanks a bunch. That solved the issue for me.