如何在matplotlib中使用分类变量和分组绘制误差条

ubbxdtey  于 2023-02-05  发布在  其他
关注(0)|答案(2)|浏览(185)

我试图得到下面的图形:
this is how to the result should look like
下面是Pandas的数据框:

df = pd.DataFrame({
    'Festzelt': ["Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt"],
    'Biermenge': [1.068510948, 1.111444388, 1.097928649, 1.097319892, 1.112046892, 1.096458863, 1.098193952, 1.105528912, 1.081012023, 1.096862587, 1.096820787, 1.112483864, 1.090409846, 1.076176749, 1.05914969, 1.111281072, 1.090280455, 1.071867235, 1.104982445, 1.074247709, 1.103154487, 1.136741808, 1.051554041, 1.089669195, 1.126347645, 1.105658808, 1.117330659, 1.101642591, 1.065208517, 1.082705561, 1.081997508, 1.100248942, 1.102306684, 1.106034801, 1.061078385, 1.065105824, 1.118714312, 1.103743509, 1.10806331, 1.127161842, 1.095313864, 1.083297614, 1.088053678, 1.096490414, 1.103947732, 1.070520785, 1.096987797, 1.045452588, 1.097941923, 1.087059407, 1.059211299, 1.004684239, 0.998865106, 1.011393955, 1.020030032, 1.000917207, 1.037486604, 1.058419981, 0.999914939, 1.037276828, 1.011935826, 1.00550927, 1.035434798, 1.049929295, 1.023505819, 1.04547058, 1.019198865, 1.01983709, 1.011544282, 1.019780386, 1.001639294, 1.027859424, 1.060448349, 1.047727746, 1.020635143, 1.030990766, 1.003855964, 1.024180945, 1.033970302, 1.024973412, 1.046135278, 1.031333533, 1.037277845, 1.023052959, 1.046540625, 1.014640256, 1.009600155, 0.988617146, 0.993939951, 1.019822804, 0.980809392, 1.034884526, 1.039759923, 1.019183791, 0.980610209, 1.015745219, 0.982644572, 1.019548832, 1.03694442, 0.984112046],
    'Gruppe': ["Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag", "Abend"]
})

下面是我的代码,但没有得到预期的结果:

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

# Define data and parameters
title = "Einzelwertdiagramm mit kategorialen Variablen und Gruppen"
colors = ["#95C13D", "#3B4D18", "#F8AB5D", "#F9B000"]

# Create a sample pandas DataFrame with categorical variables
df = pd.DataFrame({
    'Festzelt': ["Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt"],
    'Biermenge': [1.068510948, 1.111444388, 1.097928649, 1.097319892, 1.112046892, 1.096458863],
    'Gruppe': ["Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag"]
})

# Set the order of the categories in the 'group' column
unique_values = df['Festzelt'].unique()
order = unique_values

# Add grid lines with both horizontal and vertical lines
sns.set_style("whitegrid")

# Compute the standard deviation for each group
grouped = df.groupby("Gruppe")
std = grouped["Biermenge"].std().to_dict()

# Create a list of dictionaries to store the x and y values, as well as the error bars
data = []
for i, group in df.groupby("Gruppe"):
    x = group["Festzelt"].tolist()
    y = group["Biermenge"].tolist()
    y_err = [std[i] for _ in range(len(y))]
    data.append({"x": x, "y": y, "y_err": y_err})

# Create a figure and axes for the plot
fig, ax = plt.subplots(figsize=(10, 6))

# Plot the data for each group
for i, group in enumerate(data):
    ax.errorbar(group["x"], group["y"], yerr=group["y_err"], fmt="o", color=colors[i], label=df["Gruppe"].unique()[i])

# Add a title and labels to the plot
ax.set_title(title)
ax.set_xlabel("Festzelt")
ax.set_ylabel("Biermenge")

# Add a legend to the plot
ax.legend()

# Show the plot
plt.show()

我在一张图中得到了所有的组,但是我需要像示例图中那样的单独的子图。有人能帮我吗?我希望问题是清楚的🙈

jutyujz0

jutyujz01#

请尝试下面的代码,我希望它是类似于你想要的:

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

# Define data and parameters
title = "Einzelwertdiagramm mit kategorialen Variablen und Gruppen"
colors = ["#95C13D", "#3B4D18", "#F8AB5D", "#F9B000"]

# Create a sample pandas DataFrame with categorical variables
df = pd.DataFrame({
    'class': ["Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt"],
    'mean': [1.068510948, 1.111444388, 1.097928649, 1.097319892, 1.112046892, 1.096458863],
    'cat': ["Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag"],
    'sd': [0.04, 0.05, 0.06, 0.04, 0.05, 0.06]
})

# Compute the mean and standard deviation for each class
grouped = df.groupby("class")
mean = grouped["mean"].mean().to_dict()
std = grouped["sd"].mean().to_dict()

# Create a list of dictionaries to store the x and y values, as well as the error bars
data = []
for i, group in df.groupby("cat"):
    x = group["class"].tolist()
    y = group["mean"].tolist()
    y_err = [2 * std[x[j]] for j in range(len(y))]
    data.append({"x": x, "y": y, "y_err": y_err})

# Create a figure and axes for the plot
fig, ax = plt.subplots(1, 3, figsize=(15, 6), sharey=True)

# Plot the data for each group
for i, group in enumerate(data):
    ax[i].errorbar(group["x"], group["y"], yerr=group["y_err"], fmt="o", color=colors[i], markersize=12, capsize=10, label=df["cat"].unique()[i])
    ax[i].set_title(df["cat"].unique()[i])
    ax[i].set_xlabel("class")
    ax[i].set_ylabel("mean")
    ax[i].tick_params(axis='x', rotation=45)

# Add a title to the plot
fig.suptitle(title)

# Show the plot
plt.show()

输出将如下所示:

wz3gfoph

wz3gfoph2#

如果你想把这3个图分成两行,你可以使用add_subplot()并根据需要调整间距和标题。更新后的代码如下所示。希望这对你有所帮助...

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

# Define data and parameters
title = "Einzelwertdiagramm mit kategorialen Variablen und Gruppen"
colors = ["#95C13D", "#3B4D18", "#F8AB5D", "#F9B000"]

# Create a sample pandas DataFrame with categorical variables
df = pd.DataFrame({
    'Festzelt': ["Hacker-Festzelt", "Hacker-Festzelt", "Hacker-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt", "Löwenbräu-Festzelt"],
    'Biermenge': [1.068510948, 1.111444388, 1.097928649, 1.097319892, 1.112046892, 1.096458863],
    'Gruppe': ["Abend", "Vormittag", "Nachmittag", "Abend", "Vormittag", "Nachmittag"]
})

# Set the order of the categories in the 'group' column
unique_values = df['Festzelt'].unique()
order = unique_values

# Add grid lines with both horizontal and vertical lines
sns.set_style("whitegrid")

# Compute the standard deviation for each group
grouped = df.groupby("Gruppe")
std = grouped["Biermenge"].std().to_dict()

# Create a list of dictionaries to store the x and y values, as well as the error bars
data = []
for i, group in df.groupby("Gruppe"):
    x = group["Festzelt"].tolist()
    y = group["Biermenge"].tolist()
    y_err = [std[i] for _ in range(len(y))]
    data.append({"x": x, "y": y, "y_err": y_err})

# Create a figure and axes for the plot
fig = plt.figure(figsize=(10, 6)) 

# Plot the data for each group
for i, group in enumerate(data):
    fig.add_subplot(2,2,i+1)
    plt.errorbar(group["x"], group["y"], yerr=group["y_err"], fmt="o", color=colors[i], capsize=10, label=df["Gruppe"].unique()[i])
    plt.gca().title.set_text(df["Gruppe"].unique()[i])

# Add a title and labels to the plot
fig.suptitle(title, fontsize=15)
#plt.xlabel("Festzelt")
#plt.ylabel("Biermenge")
fig.text(0.5, 0.04, 'Festzelt', ha='center', va='center', fontsize=14)
fig.text(0.06, 0.5, 'Biermenge', ha='center', va='center', rotation='vertical', fontsize=14)
fig.subplots_adjust(wspace=.5)

# Show the plot
plt.show()

结果是...

相关问题