我在 python 2.7 中使用 matplotlib
我需要创建一个简单的 pyplot 条形图,对于每个条形图,我需要在它上面添加它的 y 值。
我正在使用以下代码创建条形图:
import matplotlib.pyplot as plt
barlist = plt.bar([0,1,2,3], [100,200,300,400], width=5)
barlist[0].set_color('r')
barlist[0].title("what?!")
Run Code Online (Sandbox Code Playgroud)
更改颜色有效,但对于标题,我收到以下错误:AttributeError: 'Rectangle' object has no attribute 'title'
我发现了一些问题,有关类似的问题,但他们没有用创建条形图以同样的方式,以及他们的解决方案并没有为我工作。
将条形的值添加为它们上方的标题的简单解决方案的任何想法?
谢谢!
我正在尝试在直方图顶部添加数据标签值,以尝试明显地显示频率。
这是我现在的代码,但不确定如何编码以将值放在上面:
plt.figure(figsize=(15,10))
plt.hist(df['Age'], edgecolor='white', label='d')
plt.xlabel("Age")
plt.ylabel("Number of Patients")
plt.title = ('Age Distrubtion')
Run Code Online (Sandbox Code Playgroud)
我想知道是否有人知道执行此操作的代码:

在的文档matplotlib.bar_label()中,您能用一些示例解释以下参数的用法吗?
**kwargs:任何剩余的关键字参数都会传递到 Axes.annotate。
#CODE TO PRODUCE ABOVE SHOWN CHART
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')
# Add some text for …Run Code Online (Sandbox Code Playgroud) 我有与这个问题类似的问题;我试图在 Seaborn 中合并三个图,但 y 轴上的标签未与条形对齐。
我的代码(现在是一个有效的复制粘贴示例):
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
### Generate example data
np.random.seed(123)
year = [2018, 2019, 2020, 2021]
task = [x + 2 for x in range(18)]
student = [x for x in range(200)]
amount = [x + 10 for x in range(90)]
violation = [letter for letter in "thisisjustsampletextforlabels"] # one letter labels
df_example = pd.DataFrame({
# some …Run Code Online (Sandbox Code Playgroud) 我想为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) 我有一个看起来像的数据框:
User A B C
ABC 100 121 OPEN
BCD 200 255 CLOSE
BCD 500 134 OPEN
DEF 600 125 CLOSE
ABC 900 632 OPEN
ABC 150 875 CLOSE
DEF 690 146 OPEN
Run Code Online (Sandbox Code Playgroud)
我正在尝试在“用户”列上显示计数图。代码如下:
fig, ax1 = plt.subplots(figsize=(20,10))
graph = sns.countplot(ax=ax1,x='User', data=df)
graph.set_xticklabels(graph.get_xticklabels(),rotation=90)
for p in graph.patches:
height = p.get_height()
graph.text(p.get_x()+p.get_width()/2., height + 0.1,
'Hello',ha="center")
Run Code Online (Sandbox Code Playgroud)
输出看起来像:
但是,我想用列“用户”的 value_counts 替换字符串“Hello”。当我添加代码以向图形添加标签时:
for p in graph.patches:
height = p.get_height()
graph.text(p.get_x()+p.get_width()/2., height + 0.1,
df['User'].value_counts(),ha="center")
Run Code Online (Sandbox Code Playgroud)
我得到的输出为:
我策划了catplot在seaborn这样的
import seaborn as sns
import pandas as pd
data = {'year': [2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015], 'geo_name': ['Michigan', 'Michigan', 'Michigan', 'Michigan', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Ann Arbor, MI', 'Ann Arbor, MI', 'Ann Arbor, MI', 'Ann Arbor, MI', 'Philadelphia, PA', 'Philadelphia, PA', 'Philadelphia, PA', 'Philadelphia, PA', 'Ann Arbor, MI Metro Area', 'Ann Arbor, …Run Code Online (Sandbox Code Playgroud) import matplotlib.pyplot as plt
import numpy as np
randomnums = np.random.normal(loc=9,scale=6, size=400).astype(int)+15
Run Code Online (Sandbox Code Playgroud)
输出:
array([25, 22, 19, 26, 24, 9, 19, 32, 30, 25, 29, 17, 21, 14, 17, 27, 27,
28, 17, 17, 20, 21, 16, 28, 20, 24, 15, 20, 20, 13, 33, 21, 30, 27,
8, 22, 24, 25, 23, 13, 24, 20, 16, 32, 15, 26, 34, 16, 21, 21, 28,
22, 23, 18, 20, 22, 23, 22, 23, 26, 22, 25, 19, 29, 14, 27, 21, …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) python ×10
matplotlib ×9
seaborn ×5
bar-chart ×3
histogram ×2
pandas ×2
plot ×2
python-3.x ×2
annotations ×1
heatmap ×1
numpy ×1
python-2.7 ×1
statistics ×1