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

内置数据集绘图:基于Orange数据集绘制指定样式bar chart的技术求助

Create a Bar Chart with the Orange Dataset (Matching Your Target Style)

Got it, let's break down how to build that bar chart you need using the built-in Orange dataset. I'll use Python with pandas for data handling and matplotlib.pyplot for plotting—these tools let you tweak every detail to match your reference style perfectly.

Step 1: Load and Prep the Data

First, let's pull in the dataset and get it ready. The Orange dataset tracks circumference growth over time for 5 orange trees:

import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.datasets import get_dataset

# Load the Orange dataset
orange_df = get_dataset('Orange').data

# Convert the Tree column to a string (it's a categorical ID, not a number)
orange_df['Tree'] = orange_df['Tree'].astype(str)

Step 2: Choose Your Metric (Adjust to Match Your Target Plot)

Since you didn't share the exact reference chart, I'll use a common use case: comparing the final circumference of each tree at the oldest measured age (158 days). If your target plot uses a different metric (like average circumference per tree, or grouped bars for multiple ages), just tweak this part:

# Grab the row with the maximum age for each tree (final growth measurement)
final_growth = orange_df.loc[orange_df.groupby('Tree')['age'].idxmax()]

Step 3: Build the Bar Chart with Custom Styling

Now let's create the plot with polished styling. I'll include common customizations that match typical professional bar charts—you can adjust colors, labels, and layout to mirror your reference:

# Set up the figure size (adjust based on your needs)
plt.figure(figsize=(8, 5))

# Create the bars—use a color that matches your reference (hex code or name)
bars = plt.bar(final_growth['Tree'], final_growth['circumference'], color='#2ca02c')

# Add value labels on top of each bar (super helpful for readability)
for bar in bars:
    height = bar.get_height()
    plt.text(
        bar.get_x() + bar.get_width()/2.,  # X position (center of bar)
        height,  # Y position (top of bar)
        f'{height} mm',  # Text to display
        ha='center', va='bottom'  # Alignment
    )

# Customize axis labels and title
plt.xlabel('Tree ID', fontsize=12)
plt.ylabel('Final Circumference (mm)', fontsize=12)
plt.title('Orange Tree Final Growth (158 Days)', fontsize=14, pad=15)

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

# Show the finished plot
plt.show()

Step 4: Tweak to Match Your Exact Style

If your target chart has specific features, here are quick fixes to adjust:

  • Horizontal bars: Swap plt.bar() for plt.barh() and adjust axis labels accordingly.
  • Grouped bars: Pivot the data to show multiple metrics per tree (e.g., circumference at 100 and 158 days) and use plt.bar() with an offset for each group.
  • Error bars: Calculate variability (like standard deviation of circumference across ages) and use the yerr parameter in plt.bar().
  • Color schemes: Replace the hex color with your reference's palette—use plt.colormaps() for built-in schemes or custom hex codes.

If you share more details about your reference chart (like grouped bars, specific colors, or axis labels), I can refine this code even further!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:03:20