You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Stata中无需清空数据集,基于自定义值制作分组条形图?

使用Matplotlib绘制分组条形图(无需新建数据集/变量)

直接用你给出的两组数据和时间点,无需额外创建新变量或修改现有数据,以下是Matplotlib的实现代码:

import matplotlib.pyplot as plt

# 你的原始数据
time_points = [1, 2, 3, 4, 5]
group_a = [4, 8, 2, 4, 2]
group_b = [1, 5, 2, 8, 7]

# 设置条形宽度
bar_width = 0.35

# 创建X轴基准位置
x = range(len(time_points))

# 绘制两组并排条形
plt.bar([i - bar_width/2 for i in x], group_a, width=bar_width, label='组a')
plt.bar([i + bar_width/2 for i in x], group_b, width=bar_width, label='组b')

# 配置坐标轴与标题
plt.xticks(x, time_points)
plt.xlabel('时间点')
plt.ylabel('数值')
plt.title('组a与组b的数值对比分组条形图')

# 添加图例并显示图形
plt.legend()
plt.show()

关键说明:

  • 全程复用你提供的原始变量,没有创建新的数据集或额外变量
  • 通过给两组条形的X轴位置分别加减半宽,实现分组并排的效果
  • 所有配置逻辑都基于现有变量完成,完全符合你的需求

内容的提问来源于stack exchange,提问作者Emily

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 00:52:04