0

For a given dataset I am plotting a box plot of size of object at 10 different points as below:

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

font_prop = font_manager.FontProperties( size=18)

def plot (path, name=""):
      df = pd.read_csv(path, index_col=0)
      df = df.dropna()
      Position = [1 + i // df.shape[0] for i in range(df.size)]
      df_n = [df[col] for col in df.columns]
      df_t = pd.concat(df_n).tolist()
      groups = [[] for i in range(max(Position))]
      [groups[Position[i] - 1].append(df_t[i]) for i in range(len(df_t))]

      plt.figure(figsize=(12, 5))
      plt.scatter(Position, df_t, color='g')
      b = plt.boxplot(groups, patch_artist=False)
      for median in b['medians']:
            median.set(color='r', linewidth=2)

A typical graph would be like this: enter image description here

I have 4 different datasets and I would like to present a graph where on the position axis (x axis) there will be 4 bar plots above each position. How would I modify my code to do that?

Here is the sample dataset: https://github.com/aebk2015/multipleboxplot.git

,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,Class
1,7.6,1.0,1.0,1.0,1.0,6.0,49.0,1.0,1.0,40.0,L
2,9.7,2.7,5.6,1.0,1.0,1.0,34.0,1.0,1.0,1.0,L
3,1.0,6.0,1.0,1.0,1.0,3.0,39.0,1.0,28.0,1.0,L
4,8.0,25.5,1.0,1.0,1.0,1.0,24.0,1.0,1.0,1.0,L
5,1.0,29.0,1.0,1.0,1.0,1.0,38.0,29.0,20.0,1.0,L
6,4.0,34.0,1.0,1.0,1.0,39.0,14.0,1.0,12.0,1.0,L
7,1.0,17.0,1.0,1.0,1.0,1.0,20.8,1.0,14.6,1.0,L
8,1.0,1.0,1.0,1.0,1.0,1.0,19.0,17.5,1.0,1.0,L
9,1.0,30.0,1.0,1.0,1.0,3.0,23.0,1.0,1.0,1.0,L
10,1.0,5.0,25.0,1.0,1.0,17.0,6.3,1.0,17.0,1.0,L
1,11.8,19.0,1.0,1.0,1.0,11.3,2.0,4.0,5.0,1.0,C
2,12.0,17.0,20.0,9.0,1.0,23.0,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1.0,B
6,47.0,20.0,1.0,1.0,1.0,1.0,28.0,7.7,1.0,1.0,B
7,22.0,92.0,1.0,1.0,1.0,20.0,15.0,1.0,1.0,1.0,B
8,31.0,72.0,1.0,1.0,1.0,1.0,20.0,1.0,1.0,1.0,B
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
A.E
  • 997
  • 1
  • 16
  • 33
  • 1
    This is not likely to get answered unless you add sample data (no images of data). I would used `sns.boxplot` which has a `hue=` option. A `'sample #'` column can be passed to `hue=` to separate the plots to 4 samples for each position. – Trenton McKinney Jul 11 '22 at 02:31
  • 1
    See this [answer](https://stackoverflow.com/a/21232849/7758804) to read all the files in to a single dataframe with an identifying column. – Trenton McKinney Jul 11 '22 at 03:09
  • @TrentonMcKinney Thanks for your comment. I tried seaborn but I am not sure how to define x1 and y1 in my dataset. I added a link to my sample dataset. Please see the last sentence in the question – A.E Jul 11 '22 at 06:02
  • 1
    I’m in bed, but from the look of the data, you need to .melt the dataframe. The class column should be set as id_var, the all the p columns will get melted to a long form. Once the dataframe is in a long form, it will work with seaborn – Trenton McKinney Jul 11 '22 at 06:42
  • 1
    See [code and plot](https://i.stack.imgur.com/C55GI.png). I'm not posting an answer, because everything is essentially a duplicate of other answers. However, if this is helpful, consider adding an upvote to those answer (e.g. https://stackoverflow.com/a/21232849/7758804, https://stackoverflow.com/a/36763815/7758804, and https://stackoverflow.com/a/69458847/7758804. – Trenton McKinney Jul 11 '22 at 16:26
  • 1
    thank you very much! I wish you could post your answer here so someone else can use it but I did upvote the other post. – A.E Jul 12 '22 at 00:44
  • You're welcome. Glad it worked for you. – Trenton McKinney Jul 12 '22 at 02:07

0 Answers0