admin管理员组

文章数量:1122846

Following is the portion of data I have:

Parameter Method RMSE MAE FID
DRCT CubicInterpolation 80.0887 87.9013 8375.7247
DRCT KrigingInterpolation 49.3513 70.9927 3823.2367
DRCT LSTM 84.6987 98.1393 11957
DRCT LinearInterpolation 36.752 35.6747 134.3787
DRCT PolynomialInterpolation 48.3893 70.502 6116.5867
DRCT SplineInterpolation 72.8787 45.774 41.7713
DRCT VAE 102.5967 90.9553 10570.3807
DRCT cGAN 0.3267 0.3447 0.0587
DWPT CubicInterpolation 23.2107 28.142 1088.6787
DWPT KrigingInterpolation 20.348 25.9827 842.3687
DWPT LSTM 30.4353 45.2293 2797.9993
DWPT LinearInterpolation 19.686 3.5553 7.8667
DWPT PolynomialInterpolation 11.9107 17.8107 517.4547
DWPT SplineInterpolation 7.2767 4.7913 11.3773
DWPT VAE 11.948 27.9 1162.7693
DWPT cGAN 0.2487 0.2907 0.0953

Following is the portion of data I have:

Parameter Method RMSE MAE FID
DRCT CubicInterpolation 80.0887 87.9013 8375.7247
DRCT KrigingInterpolation 49.3513 70.9927 3823.2367
DRCT LSTM 84.6987 98.1393 11957
DRCT LinearInterpolation 36.752 35.6747 134.3787
DRCT PolynomialInterpolation 48.3893 70.502 6116.5867
DRCT SplineInterpolation 72.8787 45.774 41.7713
DRCT VAE 102.5967 90.9553 10570.3807
DRCT cGAN 0.3267 0.3447 0.0587
DWPT CubicInterpolation 23.2107 28.142 1088.6787
DWPT KrigingInterpolation 20.348 25.9827 842.3687
DWPT LSTM 30.4353 45.2293 2797.9993
DWPT LinearInterpolation 19.686 3.5553 7.8667
DWPT PolynomialInterpolation 11.9107 17.8107 517.4547
DWPT SplineInterpolation 7.2767 4.7913 11.3773
DWPT VAE 11.948 27.9 1162.7693
DWPT cGAN 0.2487 0.2907 0.0953

I am trying to create a diagram to represent Mean Absolute Error (MAE) comparison for different parameters, i.e., DRCT, DWPT across different methods. The horizontal axis will be used for representing the parameters. There is no vertical axis. Instead, I am looking to create a box for each parameter. The box will represent the MAE. The bottom and top will represent the min and max of MAE for the parameter. MAE from other methods will be represented as points or lines inside the box.

Can someone help me to achieve this? I'm clueless at the moment; regular box plots won't do.

expected figure is attached below. I created it using paint. sorry for the less details.

Share Improve this question edited Nov 22, 2024 at 9:51 srinivas asked Nov 22, 2024 at 8:33 srinivassrinivas 2991 silver badge11 bronze badges 2
  • It would help to provide a schematic of the expected graph with the values (and computations) that match your example – mozway Commented Nov 22, 2024 at 9:00
  • I have updated a representative figure now. – srinivas Commented Nov 22, 2024 at 10:10
Add a comment  | 

2 Answers 2

Reset to default 0

Hopefully, this is what you wanted:

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

data = {
    'Parameter': ['DRCT', 'DRCT', 'DRCT', 'DRCT', 'DRCT', 'DRCT', 'DRCT', 'DRCT', 'DWPT', 'DWPT', 'DWPT', 'DWPT', 'DWPT', 'DWPT', 'DWPT', 'DWPT'],
    'MAE': [87.9013, 70.9927, 98.1393, 35.6747, 70.502, 45.774, 90.9553, 0.3447, 28.142, 25.9827, 45.2293, 3.5553, 17.8107, 4.7913, 27.9, 0.2907]
}

df = pd.DataFrame(data)

plt.figure(figsize=(12, 8))
sns.boxplot(x='MAE', y='Parameter', data=df, orient='h')
sns.stripplot(x='MAE', y='Parameter', data=df, color='orange', jitter=0.2, size=5)

plt.title('Horizontal Boxplot of MAE by Parameter')
plt.xlabel('MAE')
plt.ylabel('Parameter')
plt.show()

AFAIK, there is no direct way to do this, but you can use lower level matplotlib. I am assuming here that all parameters have the same methods in the same order (if not, you'll need to handle the color/legends):

import matplotlib.pyplot as plt
import matplotlib
from matplotlib.collections import LineCollection
from matplotlib.lines import Line2D

required = ['q1', 'med', 'q3', 'whislo', 'whishi']

dicts = {}
vals = []

# compute min/max for boxes
for k, g in df.set_index('Method').groupby('Parameter')['MAE']:
    dicts[k] = (g.agg(['min', 'max'])
                 .rename({'min': 'q1', 'max': 'q3'})
                 .reindex(required)
                 .to_dict()
               )
    vals.append(g)

# box width
w = 0.2
    
# plot boxes
ax = plt.subplot()
ax.bxp(dicts.values(),
       widths=np.repeat(w, len(dicts)),
       showfliers=False)
ax.set_xticklabels(list(dicts))

# plot lines
cmap = matplotlib.colormaps['tab10']
for i, v in enumerate(vals, start=1):
    lc = LineCollection([[(i-w/2, y), (i+w/2, y)] for y in v],
                        colors=cmap.colors[:len(v)]
                       )
    ax.add_collection(lc)

# add legend
colors=cmap.colors[:len(v)]
ax.legend([Line2D([0, 1], [0, 1], color=c) for c in colors],
          v.index
         )

Output:

本文标签: pythonPreparing a box diagram for representing accuracy metricsStack Overflow