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

如何用Matplotlib在Python中实现视频同款图表?求Python 3代码

Replicating the Grouped Bar Plot with Error Bars from the YouTube Video

Got it! Looking at the 847s timestamp in the video you referenced, the chart is a grouped bar plot with error bars—perfect for comparing metrics across multiple categories and groups. Below is a complete Python 3 + Matplotlib implementation that matches that visual style closely, with comments to help you tweak it to your exact data:

Full Code Snippet

import matplotlib.pyplot as plt
import numpy as np

# ----------------------
# Step 1: Prepare your data
# Replace these with your actual values from the video/your dataset
# ----------------------
category_names = ['Task A', 'Task B', 'Task C', 'Task D']
group1_averages = [82, 76, 91, 87]  # Example: Model X performance
group1_margins = [3.2, 4.1, 2.5, 3.0]  # Error (e.g., standard deviation/confidence interval)
group2_averages = [78, 84, 86, 89]  # Example: Model Y performance
group2_margins = [3.8, 3.3, 2.8, 2.2]

# ----------------------
# Step 2: Set up plot parameters
# ----------------------
bar_width = 0.35
x_coords = np.arange(len(category_names))  # Base positions for categories
fig, ax = plt.subplots(figsize=(10, 6))  # Set figure size

# ----------------------
# Step 3: Draw bars and error bars
# ----------------------
# First group of bars
bars_group1 = ax.bar(x_coords - bar_width/2, group1_averages, width=bar_width,
                     label='Model X', color='#1f77b4', alpha=0.8)
ax.errorbar(x_coords - bar_width/2, group1_averages, yerr=group1_margins,
            fmt='none', c='black', capsize=5)  # Error bars

# Second group of bars
bars_group2 = ax.bar(x_coords + bar_width/2, group2_averages, width=bar_width,
                     label='Model Y', color='#ff7f0e', alpha=0.8)
ax.errorbar(x_coords + bar_width/2, group2_averages, yerr=group2_margins,
            fmt='none', c='black', capsize=5)

# ----------------------
# Step 4: Add plot details (matches video's style)
# ----------------------
ax.set_xlabel('Task Type', fontsize=12)
ax.set_ylabel('Accuracy (%)', fontsize=12)
ax.set_title('Model Performance Comparison', fontsize=14, pad=20)
ax.set_xticks(x_coords)
ax.set_xticklabels(category_names, fontsize=10)
ax.legend(fontsize=11)

# Optional: Add value labels on top of each bar (like in the video)
def add_bar_labels(bars):
    for bar in bars:
        height = bar.get_height()
        ax.text(bar.get_x() + bar.get_width()/2., height,
                f'{height}%',
                ha='center', va='bottom', fontsize=10)

add_bar_labels(bars_group1)
add_bar_labels(bars_group2)

# Adjust layout to prevent label cutoff
plt.tight_layout()

# Show the plot
plt.show()

Customization Tips

  • More groups: If the video has 3+ groups, adjust bar_width (e.g., 0.25 for 3 groups) and add additional bar/errorbar calls with shifted x-coords (e.g., x_coords - bar_width, x_coords, x_coords + bar_width).
  • Colors: Swap the color values to match the exact hues from the video (use a color picker tool if needed).
  • Horizontal bars: Use ax.barh() instead of ax.bar() for a horizontal version—just adjust the x/y labels and coordinate logic accordingly.
  • Error bar style: Tweak capsize to make the error bar caps wider/narrower, or change c to a different color for the error lines.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 13:57:41