我想为Seaborn生成的因子图添加数据标签.这是一个例子:
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
titanic_df = pd.read_csv('train.csv')
sns.factorplot('Sex',data=titanic_df,kind='count')
Run Code Online (Sandbox Code Playgroud)
如何将"计数"值添加到图表上每个条形的顶部?
我想创建一个径向条形图。我有以下 Python3 代码:
lObjectsALLcnts = [1, 1, 1, 2, 2, 3, 5, 14, 15, 20, 32, 33, 51, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 6, 7, 7, 10, 10, 14, 14, 14, 17, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 5, 5, 6, 14, 14, 27, 27, 1, 1, 2, 3, 4, 4, 5]`
lObjectsALLlbls = ['DuctPipe', 'Column', 'Protrusion', 'Tree', 'Pole', 'Bar', 'Undefined', 'EarthingConductor', 'Grooves', 'UtilityPipe', 'Cables', 'RainPipe', 'Moulding', 'Intrusion', 'PowerPlug', 'UtilityBox', 'Balcony', …Run Code Online (Sandbox Code Playgroud) 我喜欢最近 matpolotlib 更新中 ax.bar_label 的易用性。
我热衷于隐藏低值数据标签以提高最终图中的可读性,以避免标签重叠。
如何在下面的代码中隐藏小于预定义值(这里假设小于 0.025)的标签?
df_plot = pd.crosstab(df['Yr_Lvl_Cd'], df['Achievement_Cd'], normalize='index')
ax = df_plot.plot(kind = 'bar', stacked = True, figsize= (10,12))
for c in ax.containers:
ax.bar_label(c, label_type='center', color = "white")
Run Code Online (Sandbox Code Playgroud) 我在Jupyter笔记本上有以下DataFrame,该笔记本使用seaborn的小节图进行绘制:
day_index avg_duration trips
0 0 708.852242 114586
1 1 676.702190 120936
2 2 684.572677 118882
3 3 708.925340 117868
4 4 781.767476 108036
5 5 1626.575057 43740
6 6 1729.155673 37508
daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday\n', \
'Friday', 'Saturday', 'Sunday']
plt.figure(figsize=(16,10));
sns.set_style('ticks')
ax = sns.barplot(data=dfGroupedAgg, \
x='day_index', \
y='avg_duration', \
hue='trips', \
palette=sns.color_palette("Reds_d", n_colors=7, desat=1))
ax.set_xlabel("Week Days", fontsize=18, alpha=0.8)
ax.set_ylabel("Duration (seconds)", fontsize=18, alpha=0.8)
ax.set_title("Week's average Trip Duration", fontsize=24)
ax.set_xticklabels(daysOfWeek, fontsize=16)
ax.legend(fontsize=15)
sns.despine()
plt.show()
Run Code Online (Sandbox Code Playgroud)
情节A: …
我正在创建一个像这样的条形图:
gender = ['M', 'F']
numbers = [males,females]
bars = plt.bar(gender, numbers, width=0.1, bottom=None, align='center', data=None)
for i in range(len(numbers)):
plt.annotate(str(numbers[i]), xy=(gender[i],numbers[i]))
plt.show()
Run Code Online (Sandbox Code Playgroud)
我想用来plt.annotate在栏的顶部写下确切的值。但是,该值打印在右侧。可以把它移到中心吗?
我的数据框如下所示:
Airport ATA Cost Destination Handling Custom Total Cost
0 PRG 599222 11095 20174 630491
1 LXU 364715 11598 11595 387908
2 AMS 401382 23562 16680 441623
3 PRG 599222 11095 20174 630491
Run Code Online (Sandbox Code Playgroud)
使用下面的代码,它给出了堆积的条形图:
df = df.iloc[:, 0:4]
df.plot(x='Airport', kind='barh', stacked=True, title='Breakdown of Costs', mark_right=True)
Run Code Online (Sandbox Code Playgroud)
如何在每个堆叠的条形图上添加总计(以千分之千为单位)?如何为%堆叠条形图中的每个细分添加?
我正在尝试注释使用pandas创建的堆叠水平条形图的值.目前的代码如下
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
d = {'group 1': [1, 2, 5, 7, 4, 5, 10],
'group 2': [5, 6, 1, 8, 2, 6, 2],
'group 3': [12, 2, 2, 4, 4, 8, 4]}
df = pd.DataFrame(d)
ax = df.plot.barh(stacked=True, figsize=(10,12))
for p in ax.patches:
ax.annotate(str(p.get_x()), xy=(p.get_x(), p.get_y()+0.2))
plt.legend(bbox_to_anchor=(0, -0.15), loc=3, prop={'size': 14}, frameon=False)
Run Code Online (Sandbox Code Playgroud)
问题是我使用的注释方法给出了x起点而不是每个段的值.我希望能够为每个条形的每个分段的中心注释每个分段的值.
编辑:为了清楚起见,我希望实现的是这样的,其中值为每个段水平(和垂直)居中:
任何帮助,将不胜感激.
我想向使用 plt.hist 生成的直方图添加频率标签。
这是数据:
np.random.seed(30)
d = np.random.randint(1, 101, size = 25)
print(sorted(d))
Run Code Online (Sandbox Code Playgroud)
我在 stackoverflow 上查找了其他问题,例如: 在 matplotlib 条形图上添加值标签 及其答案,但显然, plt.plot(kind='bar') 返回的对象与 plt.hist 返回的对象不同,并且正如条形图的一些答案中所建议的那样,我在使用“get_height”或“get width”函数时遇到错误。
同样,通过查看有关直方图的 matplotlib 文档也找不到解决方案。出现这个错误
您好,我正在尝试向我的 5 个类别和 2 个值(旧的和年轻的)添加百分比countplot。我尝试添加来自
如何在seaborn中的条形顶部添加百分比?
我的代码:
plt.figure(figsize =(7,5))
ax = sb.countplot(data = df_x_1, x = 'concern_virus', hue = 'age')
plt.xticks(size =12)
plt.xlabel('Level of Concern', size = 14)
plt.yticks(size = 12)
plt.ylabel('Number of People', size = 12)
plt.title("Older and Younger People's Concern over the Virus", size = 16)
ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha="right");
for p in ax.patches:
percentage = '{:.1f}%'.format(100 * p.get_height()/total)
x = p.get_x() + p.get_width()
y = p.get_height()
ax.annotate(percentage, (x, y),ha='center')
plt.show()
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,百分比没有意义。
我一直在尝试用其值来注释堆积条形图的每个子金额,如上图所示(值不准确,只是一个示例)。
df.iloc[1:].T.plot(kind='bar', stacked=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
链接的帖子与我的问题有些相似,但我不理解该答案中给出的代码,也没有给出任何解释。
matplotlib ×10
python ×10
pandas ×4
bar-chart ×3
seaborn ×3
countplot ×1
histogram ×1
plot ×1
python-3.x ×1