gpt4 book ai didi

python - Matplotlib 不显示不同语言(印地语)的字符和表情符号等其他字符

转载 作者:行者123 更新时间:2023-12-04 01:17:34 35 4
gpt4 key购买 nike

该函数只是获取一些视频的数据,并尝试在水平堆叠条形图中表示它们。参数数据是列表字典 {'key' : [ ], 'key2' : [ ].....}。下面给出了示例数据(图像中结果的数据)。出现的问题是它无法表示其中包含印地语字符和表情符号的数据(请参见 y 轴)。问题可能是因为字体的 Unicode 集。我尝试更改 yticks 的字体,但没有任何效果。

Python代码

import numpy as np
import matplotlib.pyplot as plt


def HorizontalBar(data):
category_names = ['Views', 'Likes', 'Dislikes', 'Comments']
labels = list(data.keys())
data = np.array(list(data.values()))
data_cum = data.cumsum(axis=1)
category_colors=['lightblue','#00ff00','#ff4d4d', 'yellow']
fig, ax = plt.subplots(figsize=(20, 15))
ax.invert_yaxis()
ax.set_xlim(0, np.sum(data, axis=1).max())
for i, (colname, color) in enumerate(zip(category_names, category_colors)):
widths = data[:, i]
starts = data_cum[:, i] - widths
ax.barh(labels,
widths,
left=starts,
height=0.5,
label=colname,
color=color
)
xcenters = starts + widths / 2

for y, (x, c) in enumerate(zip(xcenters, widths)):
if int(c) != 0:
ax.text(x, y, str("%.2f" % c),
ha='center', va='center',
color='k', size="x-small")
ax.legend(ncol=len(category_names))

plt.show()

enter image description here

示例数据

data={'Explore the Universe and Planets Documentary - In Search of Alien Planets in the Universe': [4.585889172397744, 2.4409090820652177, 1.6127838567197355, 0], 'Jumping From Space! - Red Bull Space Dive - BBC': [7.81020184066208, 6.029907456385649, 4.577514777773365, 4.973788033599711], 'WATCH NASA: Earth Viewing cameras - Earth From Space ISS feed #RealTimeTracker': [7.322351362215814, 5.5248075007512005, 4.199782593968985, 0], 'अंतरिक्ष के 20 Facts जो आपको किताबों में नहीं मिलेंगे 20 Space Facts You Won’t Find in Textbooks': [6.267141913650333, 4.71803642078278, 3.273464272621346, 3.2022157758011316], 'How they Eat, Drink and survive in Space ׃ Sunita Williams in The International Space Station': [6.110854582811279, 4.061716293159897, 2.8068580295188172, 2.8943160626844384], 'ब्रम्हांड और जीवन की कहानी | Universe , Space and Life Documentary in Hindi - 4K': [5.30875857554719, 3.739255803268511, 2.346352974450639, 2.429752280002408], 'Story of Man Who Fell From Space!': [6.651851911542932, 4.956221272827951, 3.6338722626583326, 4.043755126968679], 'JOURNEY TO THE EDGE OF THE Universe | Space Documentary 2020 Full HD 1080p': [5.614046768959247, 3.592398846115564, 2.4771212547196626, 2.8356905714924254], '10 INSANE Space Discoveries 2020': [4.678163150262343, 2.9258275746247424, 1.6532125137753437, 2.130333768495006], 'What Happened to Lost Women in Space?': [6.785011401802075, 4.7992922918301195, 3.7758288144646124, 4.032779879191245], 'ISS - International Space Station - Inside ISS - Tour - Q&A - HD': [6.04289786529016, 4.082534052872713, 2.60422605308447, 3.251638220448212], '10 Terrifying Facts About Space': [6.188611290819635, 4.34966598409663, 2.9628426812012423, 3.4448251995097476], 'The Discovery of the Universe Documentary - Planets of our Solar System': [3.384353414137506, 2.24551266781415, 1.5440680443502757, 0], 'I Went To SPACE In GTA 5.. (GTA 5 Mods)': [6.123185320714601, 4.776505053570831, 2.9916690073799486, 3.485295438726089], 'A Mysterious Journey to the End of Space': [5.731390507617008, 0, 0, 3.2430380486862944], 'Honey in space': [7.417521845922459, 5.709508248618531, 3.918344928962275, 4.468022226494091], 'WHAT IS THE EVERYDAY LIFE OF AN ASTRONAUT IN SPACE LIKE?': [6.803599467158072, 4.530276557681542, 3.255272505103306, 3.6644539285811577], "In 2009 Something Happened In This Region Of Space That NASA Scientists Can't Explain": [4.18261434773635, 2.9951962915971793, 1.7558748556724915, 2.103803720955957], 'The Search for Life in Space Documentary - Where is the Centre of the Universe': [4.259402728142589, 2.2329961103921536, 1.255272505103306, 0], 'The Milky Way Galaxy Planets | Space Documentary 2020 Full HD 1080p': [5.701836422125923, 3.5258219521566625, 2.705007959333336, 2.6190933306267428], "The World's Biggest Skyscrapers (Some Will Even Reach Space!)": [5.146630285904248, 0, 0, 2.885361220031512], 'Why There Is Light on Earth But Not in Space': [6.276289660766413, 4.5772159697346515, 3.119585774961784, 3.447623097760286], "Spacewalk to Upgrade Space Station's Power System": [5.486618892027429, 4.018201022496291, 2.346352974450639, 2.315970345456918], 'What Happened to Felicette in Space? *First Cat in Space*': [5.801479173275887, 4.116508123737346, 2.932473764677153, 3.705949194910296], 'What If You Were the Last Human and Trapped in Space?': [5.7236830219147246, 4.179523067035285, 2.5622928644564746, 3.209783014848515], '10 Scary Yet Beautiful Facts About Space & Us': [7.303539813426686, 5.52070226250271, 4.194764024110887, 4.712977823472636], '15 STRANGEST Things Caught From Space!': [5.478580923742277, 3.7083359026822635, 2.584331224367531, 2.5538830266438746], 'What Happened to Ham in Space? *Sad Story of Ham*': [7.086435398459355, 5.265023120238883, 3.8825245379548803, 4.456745495347945], 'What Happened to the People Lost in Space?': [6.285491597306807, 4.41806916354166, 3.131297796597623, 3.210318519826232], 'My Space - Don Omar ft. Wisin y Yandel': [7.925400999293699, 5.399019191395705, 4.107142105833073, 3.734559821579476], '15 Space saving furniture ideas for your home Live Smart & Expand Your Space': [7.516409324272038, 5.195096710611856, 4.228400358703005, 3.641077313325374], 'I Found A STAIRWAY To SPACE In GTA 5.. (GTA 5 Mods)': [6.2833840081994925, 5.104657791008797, 3.318272080211627, 3.6464037262230695], 'is Space Shuttle Aluminum Bulletproo ? 🚀✈️': [5.326947948497112, 4.2316989107643925, 2.4983105537896004, 3.0827853703164503], 'Russia ने किया Space में हतियार Test': [4.13510083376572, 3.097604328874411, 1.4913616938342726, 1.7558748556724915], "The View from Space - Earth's Countries and Coastlines": [7.086502021039129, 4.63181904831605, 4.299572666758495, 4.092896010921856], 'What Will You Look Like in 4th Dimensional Space': [5.273542933650583, 3.734319680859007, 2.2576785748691846, 2.9537596917332287], 'StoryBots Outer Space | Planets, Sun, Moon, Earth and Stars | Solar System Super Song | Fun Learning': [7.578386260048627, 4.983797626014332, 4.229041573173397, 2.7024305364455254], 'I Wanna Be An Astronaut! | A Space Song |  Little Angel Kids Songs': [7.502222939931596, 5.105847270810036, 4.874133778827972, 0], 'Dove Cameron, Sofia Carson - Space Between (from Descendants 2) (Official Video)': [7.797358061362572, 5.722391209132822, 4.12251077061032, 4.256549382152194], 'NEWS: UK and US say Russia Fired A Satellite Weapon In Space | G I Joe Movie |': [4.104828403653656, 3.144262773761991, 1.3424226808222062, 2.0453229787866576], '10 Most MYSTERIOUS Photos Ever Taken From Space': [5.959342907535137, 3.7247672456463103, 2.7403626894942437, 2.7315887651867388], 'Planet Earth seen from space (Full HD 1080p) ORIGINAL': [7.39847202348151, 4.9780891730561425, 3.8489892062511672, 4.2499806059095295], 'Unbelievably Strange Planets in Space': [6.988510530789991, 5.113445298538364, 4.139753185695353, 4.2003579455416356], 'Spacewalk Outside the International Space Station': [5.685139577231512, 4.199618067707931, 2.4533183400470375, 2.7558748556724915], '15 FUTURE Space Station Design Concepts': [3.789439684567179, 2.4183012913197452, 1.0791812460476249, 1.462397997898956], 'A Record-Tying Spacewalk to Upgrade the Space Station on This Week @NASA – July 24, 2020': [4.650336669434548, 3.396896449142524, 1.6989700043360187, 0], 'SpaceX astronauts speak to Yahoo Finance from International Space Station': [3.918764031027999, 1.6720978579357175, 1.0791812460476249, 1.0], 'What If You Shoot a Gun in Space?': [5.8650020545642425, 4.24206911233894, 2.7701152947871015, 3.159266331093494], "The Magnetic Universe — it's Electric! | Space News": [3.9586594270529334, 3.0916669575956846, 1.146128035678238, 2.3654879848909], 'Teen Titans Go! | Space Adventures! | DC Kids': [7.184527134243162, 4.79228660554035, 4.018284308426531, 0]}

最佳答案

  • 使用 Pandas 的替代选项
  • 指定 TrueType 字体,然后设置 FontProperties .
    • 可以使用字体文件的绝对路径指定字体,方法是使用 fname kwarg。然而,在这种情况下,将路径(作为 pathlib.Path,而不是 str)传递给 Textfont kwarg 通常更简单。对象。
  • 基本上,为了将特定类型的字体显示为图形刻度标签,matplotlib 必须配置为使用正确的字体类型。
  • 使用来自 OP 的数据
  • 有趣的是,如果您查看 df 的列名称,印地语字符和表情符号会正确显示。
  • 此外,如果您查看列表 text,所有字符都会正确显示。
  • 我无法让表情符号正确显示在绘图上。
import pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path
from matplotlib.font_manager import FontProperties

# point to the font location with an absolute path
nirm = Path('c:/Windows/Fonts/Nirmala.ttf')

# configure the Hindi font
hindi_font = FontProperties(fname=nirm)

# create the dataframe
df = pd.DataFrame(data, index=['Views', 'Likes', 'Dislikes', 'Comments'])

# pandas.DataFrame.plot uses the index for the axis so Transpose df and then plot
ax = df.T.plot.barh(stacked=True, figsize=(10, 14))

# get the y tick labels
text = [item.get_text() for item in ax.get_yticklabels()]

# set the labels with the ttf
ax.set_yticklabels(text, fontproperties=hindi_font)

# annotate the bars
for rect in ax.patches:
# Find where everything is located
height = rect.get_height()
width = rect.get_width()
x = rect.get_x()
y = rect.get_y()

# The width of the bar is the count value and can used as the label
label_text = f'{width:.02f}'

label_x = x + width / 2
label_y = y + height / 2

# don't include label if it's equivalently 0
if width > 0.001:
ax.text(label_x, label_y, label_text, ha='center', va='center', fontsize=8)

plt.show()

enter image description here

关于python - Matplotlib 不显示不同语言(印地语)的字符和表情符号等其他字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63101803/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com