以下是一个示例代码,演示如何在条形图上隐藏百分比标签:
import matplotlib.pyplot as plt
# 创建示例数据
categories = ['A', 'B', 'C', 'D', 'E']
values = [20, 35, 30, 10, 5]
# 创建条形图
fig, ax = plt.subplots()
bars = ax.bar(categories, values)
# 隐藏百分比标签
for bar in bars:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width() / 2, height,
f'{height}%', ha='center', va='bottom')
# 显示图形
plt.show()
在这个例子中,我们使用matplotlib
库创建了一个简单的条形图。然后,使用ax.text()
方法在每个条形上添加了一个百分比标签。如果要隐藏百分比标签,我们可以将ax.text()
的调用删除或注释掉。
请注意,这只是一个简单的示例代码,你可能需要根据自己的需求进行调整和修改。
上一篇:百分比变量的反馈控制器