- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
该函数只是获取一些视频的数据,并尝试在水平堆叠条形图中表示它们。参数数据是列表字典 {'key' : [ ], 'key2' : [ ].....}
。下面给出了示例数据(图像中结果的数据)。出现的问题是它无法表示其中包含印地语字符和表情符号的数据(请参见 y 轴)。问题可能是因为字体的 Unicode 集。我尝试更改 yticks 的字体,但没有任何效果。
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()
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]}
最佳答案
FontProperties
.
fname
kwarg。然而,在这种情况下,将路径(作为 pathlib.Path
,而不是 str
)传递给 Text 的 font
kwarg 通常更简单。对象。数据
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()
关于python - Matplotlib 不显示不同语言(印地语)的字符和表情符号等其他字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63101803/
我无法在此图中定位轴标签。我喜欢放置顶部标签,使管道与网格对齐,并放置左右标签,以便它们不接触绘图。 我试过了 ax.tick_params(axis='both', which='both'
我使用的是 python 2,下面的代码只是使用了一些示例数据,我的实际数据可能有不同的长度,并且可能不是很细。 import numpy as np import datetime i
给定坐标 [1,5,7,3,5,10,3,6,8]为 matplotlib.pyplot ,如何突出显示或着色线条的不同部分。例如,列表中的坐标 1-3 ( [1,5,7,3] ) 表示属性 a .我
我正在matplotlib中绘制以下图像。 我的问题是,图像看起来像这样,但是,我想使背景变暗,因为当我打印该图像时,灰度部分不会出现在打印物中。有人可以告诉我API进行此更改吗? 我使用简单的API
这是关于matplotlib的一个非常基本的问题,但是我不知道该怎么做: 我想绘制多个图形,并使用绘制窗口中的箭头从一个移到另一个。 目前,我只知道如何创建多个图并将其绘制在不同的窗口中,如下所示:
在 matplotlib 中绘制小块对象时,由于显示分辨率而引入了伪影。使用抗锯齿并不能解决问题。 这个问题有解决方案吗? import matplotlib.pyplot as plt impo
对于直方图,有一个简单的内置选项 histtype='step' .如何制作相同风格的条形图? 最佳答案 [阅读评论后添加答案] 将可选关键字设置为 fill=False对于条形图: import m
我正在尝试在 (6X3) 网格上创建子图。我对图例的位置有疑问。图例对所有子图都是通用的。 lgend 现在与 y 轴标签重叠 我尝试删除 constrained_layout=True 选项。但这在
我有一个带有一些线段( LineCollection )和一些点的图表。这些线和点有一些与它们相关的值,但没有绘制出来。我希望能够添加鼠标悬停工具提示或其他方法来轻松找到点和线的关联值。这对于点或线段
我想创建一个带有对齐不同曲线文本的图例的图。这是一个最小的工作示例: import matplotlib.pyplot as plt import numpy as np x=np.linspace(
可以说我正在用matplotlib绘制一条线并添加一个图例。 在图例中,其显示为------ Label。当绘制较小的图形尺寸以进行打印时,我发现该行的默认水平长度太长。 是否存在将------ La
我正在使用 matplotlib 构建一个 3D 散点图,但无法使生成的图形具有所有 3 个轴的共同原点。我怎样才能做到这一点? 我的代码(到目前为止),我还没有为轴规范实现任何定义,因为我对 Pyt
我有一个我想使用的绘图布局,其中 9 个不同的数据簇被布置在一个方形网格上。网格中的每个框都包含 3 个并排布置的箱线图。 我最初的想法是这将适合 3x3 子图布局,每个单独的子图本身被划分为 3x1
我的图形从y=-1变为y=10 我想在任意位置写一小段文字,例如x=2000,y=5: ax.annotate('MgII', xy=(2000.0, 5.0), xycoords='data')
我想使用LateX格式来构建一个表达式,其中出现一些数字,但这些数字是用LateX表达式中的变量表示的。 实际的目标是在axes.annotate()方法中使用它,但是为了讨论起见,这里是一个原理代码
我需要比较两组的二维分布。 当我使用 matplotlib.pyplot.contourf并覆盖图,每个等高线图的背景颜色填充整个图空间。有没有办法让每个等高线图的最低等高线级别透明,以便更容易看到每
在R中,有一个locator函数,类似于Matlab的ginput,您可以用鼠标单击图形并选择任何x,y坐标。此外,还有一个名为identify(x,y)的函数,如果您给它绘制了一组绘制的点x,y,然
我想用matplotlib生成矢量图。我尽力了-但输出是光栅图像。这是我使用的: import matplotlib matplotlib.use('Agg') import matplotlib.p
我正在尝试使用 matplotlib 制作具有非常小的灰点的散点图。由于点密度的原因,点需要很小。问题是 scatter() 函数的标记似乎既有线条又有填充。当标记很小时,只有线条可见,而看不到填充,
我不太明白为什么我无法在指定的限制内创建水平和垂直线。我想用这个框绑定(bind)数据。然而,双方似乎并没有遵守我的指示。为什么是这样? # CREATING A BOUNDING BOX # BOT
我是一名优秀的程序员,十分优秀!