我试图得到下面的图形:
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()
我在一张图中得到了所有的组,但是我需要像示例图中那样的单独的子图。有人能帮我吗?我希望问题是清楚的🙈
2条答案
按热度按时间jutyujz01#
请尝试下面的代码,我希望它是类似于你想要的:
输出将如下所示:
wz3gfoph2#
如果你想把这3个图分成两行,你可以使用
add_subplot()
并根据需要调整间距和标题。更新后的代码如下所示。希望这对你有所帮助...结果是...