python - How to include wingdings 3 characters in a matplotlib data table? - Stack Overflow

I need to include arrows in a data table. I have achieved it successfully with some simple arrow charac

I need to include arrows in a data table. I have achieved it successfully with some simple arrow characters (e.g. unicode '\u2191' for an up arrow). But the fonts that I have access to are limiting me from using some of the more detailed / interesting arrows.

This has led to me trying to use wingdings, which is installed on my system. In MS Word I can type 'kmg' - changing this text font to 'wingdings 3' makes them appear as arrows.

My attempt in the code below results in errors such as:

67: UserWarning: Glyph 112 (p) missing from font(s) Wingdings 3

861: UserWarning: Glyph 109 (m) missing from font(s) Wingdings 3

And the bottom row in the table us filled with only error boxes. Please can anyone guide me?

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


df = pd.DataFrame({"Before":[1,4,7,5,8,2,5,7], "After":[2,7,9,1,9,2,7,3]})
df["Change"]=df["After"]-df["Before"]


#Define arrow symbols

up_arrow = 'k'  # Up arrow
down_arrow = 'm'  # Down arrow
right_arrow = 'g'  # Right arrow


#Create the arrchg row based on Change values

arrchg_row = []
for change in df["Change"]:
    if change > 0:
        arrchg_row.append(up_arrow)
    elif change < 0:
        arrchg_row.append(down_arrow)
    else:
        arrchg_row.append(right_arrow)


#Prepare the cell text for the table

cell_text = [df["Before"].values, df["After"].values, df["Change"].values, arrchg_row]


#Create the plot

fig, ax = plt.subplots()


#Hide axes

fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')


#Create the table

rect=ax.table(
    cellText=cell_text,
    rowLabels=["Bef", "Arf", "chg", "arrchg"],
    loc='lower center',  # May need adjusting to 'lower center' or 'upper center' in Power BI
    cellLoc='center'
    )


#Set font properties for the first three rows

for (i, j), cell in rect.get_celld().items():
    if i < 3:  # First three rows
        cell.set_fontsize(12)
        cell.set_text_props(fontname='Arial')  # Change to your desired font
    else:  # Last row with Wingdings
        cell.set_fontsize(12)
        cell.set_text_props(fontname='Wingdings 3')


fig.tight_layout()
plt.show()

I need to include arrows in a data table. I have achieved it successfully with some simple arrow characters (e.g. unicode '\u2191' for an up arrow). But the fonts that I have access to are limiting me from using some of the more detailed / interesting arrows.

This has led to me trying to use wingdings, which is installed on my system. In MS Word I can type 'kmg' - changing this text font to 'wingdings 3' makes them appear as arrows.

My attempt in the code below results in errors such as:

67: UserWarning: Glyph 112 (p) missing from font(s) Wingdings 3

861: UserWarning: Glyph 109 (m) missing from font(s) Wingdings 3

And the bottom row in the table us filled with only error boxes. Please can anyone guide me?

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


df = pd.DataFrame({"Before":[1,4,7,5,8,2,5,7], "After":[2,7,9,1,9,2,7,3]})
df["Change"]=df["After"]-df["Before"]


#Define arrow symbols

up_arrow = 'k'  # Up arrow
down_arrow = 'm'  # Down arrow
right_arrow = 'g'  # Right arrow


#Create the arrchg row based on Change values

arrchg_row = []
for change in df["Change"]:
    if change > 0:
        arrchg_row.append(up_arrow)
    elif change < 0:
        arrchg_row.append(down_arrow)
    else:
        arrchg_row.append(right_arrow)


#Prepare the cell text for the table

cell_text = [df["Before"].values, df["After"].values, df["Change"].values, arrchg_row]


#Create the plot

fig, ax = plt.subplots()


#Hide axes

fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')


#Create the table

rect=ax.table(
    cellText=cell_text,
    rowLabels=["Bef", "Arf", "chg", "arrchg"],
    loc='lower center',  # May need adjusting to 'lower center' or 'upper center' in Power BI
    cellLoc='center'
    )


#Set font properties for the first three rows

for (i, j), cell in rect.get_celld().items():
    if i < 3:  # First three rows
        cell.set_fontsize(12)
        cell.set_text_props(fontname='Arial')  # Change to your desired font
    else:  # Last row with Wingdings
        cell.set_fontsize(12)
        cell.set_text_props(fontname='Wingdings 3')


fig.tight_layout()
plt.show()
Share asked Mar 14 at 14:01 BaldyBaldy 575 bronze badges 2
  • Those symbols have unicode analogues. Try using \u2197 instead of k, \u2198 instead of m and \u2192 instead of g. – Man made of meat Commented Mar 14 at 16:13
  • Thank you. Unfortunately this results in a similar error: py:861: UserWarning: Glyph 8599 (\N{NORTH EAST ARROW}) missing from font(s) Wingdings 3. – Baldy Commented Mar 17 at 10:19
Add a comment  | 

1 Answer 1

Reset to default 0

Try unicode:

up_arrow = '\u2197'  # Up arrow
down_arrow = '\u2198'  # Down arrow
right_arrow = '\u2192'  # Right arrow

Comment out (or delete) the following:

#Set font properties for the first three rows
#for (i, j), cell in rect.get_celld().items():
#    if i < 3:  # First three rows
#        cell.set_fontsize(12)
#        cell.set_text_props(fontname='Arial')  # Change to your desired fon    t
#    else:  # Last row with Wingdings
#        cell.set_fontsize(12)
#        cell.set_text_props(fontname='Wingdings 3')

The output:

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744652385a4585977.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信