You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用Seaborn Hue Barplot Map复现R脚本生成的目标图表

Recreate R-style Bar Plot with Seaborn Hue Parameter

Got it, let's walk through how to build that bar plot with Seaborn, matching the style you had in your R script. We'll use the hue parameter to differentiate between the two days (Lundi and Mardi) and structure everything step by step.

Step 1: Set Up Dependencies & Data

First, we'll need to import the required libraries and load your data into a pandas DataFrame—Seaborn plays nicely with pandas, so this is the first logical step.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Your raw data converted to a list of lists
data = [
    [0, 0, 270.5, "Lundi"],
    [0, 1, 275.0, "Lundi"],
    [0, 2, 270.0, "Lundi"],
    [0, 3, 273.0, "Lundi"],
    [0, 4, 268.0, "Lundi"],
    [0, 5, 272.0, "Lundi"],
    [0, 6, 269.0, "Lundi"],
    [0, 7, 270.0, "Lundi"],
    [0, 8, 269.0, "Lundi"],
    [0, 9, 274.0, "Lundi"],
    [0, 10, 274.0, "Lundi"],
    [0, 11, 274.0, "Lundi"],
    [0, 12, 273.0, "Lundi"],
    [0, 13, 271.0, "Lundi"],
    [0, 14, 377.0, "Lundi"],
    [0, 15, 2685.0, "Lundi"],
    [0, 16, 2654.0, "Lundi"],
    [0, 17, 2706.0, "Lundi"],
    [0, 18, 2751.0, "Lundi"],
    [0, 19, 2325.0, "Lundi"],
    [0, 20, 274.0, "Lundi"],
    [0, 21, 273.0, "Lundi"],
    [0, 22, 277.0, "Lundi"],
    [0, 23, 276.0, "Lundi"],
    [1, 0, 272.5, "Mardi"],
    [1, 1, 274.0, "Mardi"],
    [1, 2, 266.0, "Mardi"],
    [1, 3, 266.0, "Mardi"],
    [1, 4, 267.0, "Mardi"],
    [1, 5, 274.0, "Mardi"],
    [1, 6, 269.0, "Mardi"],
    [1, 7, 266.0, "Mardi"],
    [1, 8, 270.0, "Mardi"],
    [1, 9, 1267.0, "Mardi"],
    [1, 10, 2618.0, "Mardi"],
    [1, 11, 2610.0, "Mardi"],
    [1, 12, 2629.0, "Mardi"],
    [1, 13, 2248.0, "Mardi"],
    [1, 14, 1897.0, "Mardi"]
]

# Convert to a structured DataFrame
df = pd.DataFrame(data, columns=["DAY", "HOUR", "VALEUR_POINT", "NAMEDAY"])

Step 2: Build the Seaborn Bar Plot with Hue

Now we'll use sns.barplot() with the hue parameter to group bars by day name. We'll also tweak the plot to make it readable—note that some VALEUR_POINT values are extremely large, which will skew the y-axis (we'll cover fixing that below).

# Set a clean style that aligns with common R plot aesthetics
sns.set_style("whitegrid")

# Create a larger figure for better readability
plt.figure(figsize=(14, 8))
ax = sns.barplot(
    x="HOUR", 
    y="VALEUR_POINT", 
    hue="NAMEDAY", 
    data=df,
    palette="Set2"  # Swap this for any Seaborn palette to match your R plot's colors
)

# Add descriptive labels and a title
ax.set_title("VALEUR_POINT by Hour and Day", fontsize=16)
ax.set_xlabel("Hour of Day", fontsize=12)
ax.set_ylabel("VALEUR_POINT", fontsize=12)

# Move the legend to a non-intrusive position
ax.legend(title="Day", loc="upper left", bbox_to_anchor=(1, 1))

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

# Display the plot
plt.show()

Quick Note on Your Data

You'll notice the y-axis is dominated by those extremely high VALEUR_POINT values (like 2685 for Lundi, 2618 for Mardi). If these are data entry errors, you can filter them out before plotting with a quick line of code:

# Filter out values above a reasonable threshold (e.g., 500)
filtered_df = df[df["VALEUR_POINT"] < 500]

Just use filtered_df in the sns.barplot() call to get a clearer view of the typical hourly values.

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

火山引擎 最新活动