如何水平居中条形图注释
How to horizontally center a bar plot annotation
我正在创建这样的条形图:
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()
我想使用 plt.annotate
在栏的顶部写上准确的值。但是,该值打印在右侧。可以移到中心吗?
- 从
matplotlib 3.4.2
,使用 matplotlib.pyplot.bar_label
。
- 请参阅此 answer 以获得详尽的解释和其他示例。
绘制列表和注释
gender = ['M', 'F']
numbers = [1644, 1771]
plt.figure(figsize=(12, 6))
p = plt.bar(gender, numbers, width=0.1, bottom=None, align='center', data=None)
plt.bar_label(p)
plt.show()
绘图 pandas 并注释
- 将列表转换为数据框并使用
pandas.DataFrame.plot
绘图
df = pd.DataFrame({'value': numbers, 'gender': gender})
ax = df.plot(x='gender', kind='bar', figsize=(12, 6), rot=0, legend=False, align='center', width=0.1)
ax.bar_label(ax.containers[0])
plt.show()
原答案
- 为了指定注释的水平对齐,使用
ha
参数
- 根据JohanC的建议
- 一个技巧是使用
f'{value}\n'
作为字符串和未修改的 value
(或 numbers
)作为 y 位置,连同 va='center'
.
- 这也适用于
plt.text
。或者,plt.annotation
接受 'points' 或 'pixels'. 中的偏移量
选项 1
- 来自
lists
个值和类别
import matplotlib.pyplot as plt
gender = ['M', 'F']
numbers = [1644, 1771]
plt.figure(figsize=(12, 6))
bars = plt.bar(gender, numbers, width=0.1, bottom=None, align='center', data=None)
for i in range(len(numbers)):
plt.annotate(f'{numbers[i]}\n', xy=(gender[i], numbers[i]), ha='center', va='center')
选项 2
- 来自
pandas.DataFrame
- 使用
pandas.DataFrame.iterrows
提取注释所需的x
和y
位置。
x
是分类 'gender'
值
y
是数字 'value'
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'value': [1771, 1644], 'gender': ['F', 'M']})
plt.figure(figsize=(12, 6))
bars = plt.bar(df.gender, df.value, width=0.1, bottom=None, align='center', data=None)
for idx, (value, gender) in df.iterrows():
plt.annotate(f'{value}\n', xy=(gender, value), ha='center', va='center')
绘图输出
我正在创建这样的条形图:
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()
我想使用 plt.annotate
在栏的顶部写上准确的值。但是,该值打印在右侧。可以移到中心吗?
- 从
matplotlib 3.4.2
,使用matplotlib.pyplot.bar_label
。- 请参阅此 answer 以获得详尽的解释和其他示例。
绘制列表和注释
gender = ['M', 'F']
numbers = [1644, 1771]
plt.figure(figsize=(12, 6))
p = plt.bar(gender, numbers, width=0.1, bottom=None, align='center', data=None)
plt.bar_label(p)
plt.show()
绘图 pandas 并注释
- 将列表转换为数据框并使用
pandas.DataFrame.plot
绘图
df = pd.DataFrame({'value': numbers, 'gender': gender})
ax = df.plot(x='gender', kind='bar', figsize=(12, 6), rot=0, legend=False, align='center', width=0.1)
ax.bar_label(ax.containers[0])
plt.show()
原答案
- 为了指定注释的水平对齐,使用
ha
参数 - 根据JohanC的建议
- 一个技巧是使用
f'{value}\n'
作为字符串和未修改的value
(或numbers
)作为 y 位置,连同va='center'
. - 这也适用于
plt.text
。或者,plt.annotation
接受 'points' 或 'pixels'. 中的偏移量
- 一个技巧是使用
选项 1
- 来自
lists
个值和类别
import matplotlib.pyplot as plt
gender = ['M', 'F']
numbers = [1644, 1771]
plt.figure(figsize=(12, 6))
bars = plt.bar(gender, numbers, width=0.1, bottom=None, align='center', data=None)
for i in range(len(numbers)):
plt.annotate(f'{numbers[i]}\n', xy=(gender[i], numbers[i]), ha='center', va='center')
选项 2
- 来自
pandas.DataFrame
- 使用
pandas.DataFrame.iterrows
提取注释所需的x
和y
位置。x
是分类'gender'
值y
是数字'value'
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'value': [1771, 1644], 'gender': ['F', 'M']})
plt.figure(figsize=(12, 6))
bars = plt.bar(df.gender, df.value, width=0.1, bottom=None, align='center', data=None)
for idx, (value, gender) in df.iterrows():
plt.annotate(f'{value}\n', xy=(gender, value), ha='center', va='center')