rm.vale
rm.vale

Reputation: 11

Multiple boxplot in a single Graphic in Python

I'm a beginner in Python.

In my internship project I am trying to plot bloxplots from data contained in a csv

I need to plot bloxplots for each of the 4 (four) variables showed above (AAG, DENS, SRG e RCG). Since each variable presents values ​​in the range from [001] to [100], there will be 100 boxplots for each variable, which need to be plotted in a single graph as shown in the image.

This is the graph I need to plot, but for each variable there will be 100 bloxplots as each one has 100 columns of values:

Graph

The x-axis is the "Year", which ranges from 2025 to 2030, so I need a graph like the one shown in figure 2 for each year and the y-axis is the sets of values ​​for each variable.

Using Pandas-melt function and seaborn library I was able to plot only the boxplots of a column. But that's not what I need:

import pandas as pd
import seaborn as sns
 
df = pd.read_csv("2DBM_50x50_Central_Aug21_Sim.cliped.csv")
mdf= df.melt(id_vars=['Year'], value_vars='AAG[001]')
print(mdf)
ax=sns.boxplot(x='Year', y='value',width = 0.2, data=mdf)

Result of the code above:

Result

What can I try to resolve this?

Upvotes: 0

Views: 5816

Answers (1)

H4iku
H4iku

Reputation: 672

The following code gives you five subplots, where each subplot only contains the data of one variable. Then a boxplot is generated for each year. To change the range of columns used for each variable, change the upper limit in var_range = range(1, 101), and to see the outliers change showfliers to True.

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

df = pd.read_csv("2DBM_50x50_Central_Aug21_Sim.cliped.csv")

variables = ["AAG", "DENS", "SRG", "RCG", "Thick"]
period = range(2025, 2031)
var_range = range(1, 101)

fig, axes = plt.subplots(2, 3)
flattened_axes = fig.axes
flattened_axes[-1].set_visible(False)

for i, var in enumerate(variables):
    var_columns = [f"TB_acc_{var}[{j:05}]" for j in var_range]
    data = df.melt(id_vars=["Period"], value_vars=var_columns, value_name=var)
    ax = flattened_axes[i]
    sns.boxplot(x="Period", y=var, width=0.2, data=data, ax=ax, showfliers=False)

plt.tight_layout()
plt.show()

output: enter image description here

Upvotes: 2

Related Questions