最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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:

发布评论

评论列表(0)

  1. 暂无评论