文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

使用Python实现嵌套绘图并为条形图添加自定义标注

程序人生路

程序人生路

2024-04-02 17:21

关注

这篇文章将为大家详细讲解有关使用Python实现嵌套绘图并为条形图添加自定义标注,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

导入库

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

准备数据

# 创建数据框
df = pd.DataFrame({
    "班级": ["A", "B", "C"],
    "数学": [80, 90, 70],
    "英语": [85, 80, 95],
    "科学": [90, 75, 85]
})

嵌套绘图

要创建嵌套绘图,首先需要创建一个主图,然后将子图添加到主图中。

# 创建主图
fig, ax = plt.subplots()

# 添加子图
ax_math = ax.bar(df["班级"], df["数学"], color="blue")
ax_english = ax.bar(df["班级"], df["英语"], color="red", bottom=df["数学"])
ax_science = ax.bar(df["班级"], df["科学"], color="green", bottom=df["数学"]+df["英语"])

自定义标注

为了添加自定义标注,可以使用 matplotlib.text 函数。

# 为每个条形图添加文本标注
for bar in ax_math:
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2, height, f"{int(height)}", ha="center", va="bottom")

for bar in ax_english:
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2, height, f"{int(height)}", ha="center", va="bottom")

for bar in ax_science:
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2, height, f"{int(height)}", ha="center", va="bottom")

图例

# 添加图例
plt.legend((ax_math[0], ax_english[0], ax_science[0]), ("数学", "英语", "科学"))

显示图表

plt.show()

完整代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    "班级": ["A", "B", "C"],
    "数学": [80, 90, 70],
    "英语": [85, 80, 95],
    "科学": [90, 75, 85]
})

fig, ax = plt.subplots()

ax_math = ax.bar(df["班级"], df["数学"], color="blue")
ax_english = ax.bar(df["班级"], df["英语"], color="red", bottom=df["数学"])
ax_science = ax.bar(df["班级"], df["科学"], color="green", bottom=df["数学"]+df["英语"])

for bar in ax_math:
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2, height, f"{int(height)}", ha="center", va="bottom")

for bar in ax_english:
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2, height, f"{int(height)}", ha="center", va="bottom")

for bar in ax_science:
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2, height, f"{int(height)}", ha="center", va="bottom")

plt.legend((ax_math[0], ax_english[0], ax_science[0]), ("数学", "英语", "科学"))

plt.show()

以上就是使用Python实现嵌套绘图并为条形图添加自定义标注的详细内容,更多请关注编程学习网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     447人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     208人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     171人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     250人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     79人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯