Dot-plot/Ggplot with Pandas DataFrame; Column names as x-values and and the corresponding columns values as y-values

Viewed 1147

I am trying to create a dot-plot/ggplot in Python for a DataFrame looking like this:

layers_time = {'2 layers': [20.6885, 25.1765, 18.7503, 19.2090, 19.2548],
               '3 layers': [20.4360, 27.5285, 20.1958, 18.9536, 20.1673],
               '4 layers': [18.1703, 21.9665, 17.0667, 17.3353, 16.2638],
               '5 layers': [19.3265, 25.7931, 17.9827, 18.5448, 18.8970],
               '6 layers': [19.7180, 24.7097, 20.7460, 19.7300, 18.2760]
                }

layers_time = pd.DataFrame(layers_time, columns=['2 layers', '3 layers', 
                           '4 layers', '5 layers', '6 layers'])

All I want is for the column names to be the x-values and their corresponding values as dots on the y-axis.
Also, is there a smart way to include the average of those values as a dot with a different color?
I've tried answers from similar questions, but they do not work as I have different x and y sizes?

2 Answers

Lets try melt the dataframe and then plot the scatter with variable as x and values as y

df=layers_time.melt()#.
plt.scatter(df.variable,df.value)

enter image description here

Answer

You can evaluate means for each column with df.mean(axis = 0).
Regarding dot-plot, you can do it with seaborn.stripplot() (or seaborn.swarmplot()) but, before that, you need to reshape your dataframe through pandas.melt().

Code

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

# data
layers_time = {'2 layers': [20.6885, 25.1765, 18.7503, 19.2090, 19.2548],
               '3 layers': [20.4360, 27.5285, 20.1958, 18.9536, 20.1673],
               '4 layers': [18.1703, 21.9665, 17.0667, 17.3353, 16.2638],
               '5 layers': [19.3265, 25.7931, 17.9827, 18.5448, 18.8970],
               '6 layers': [19.7180, 24.7097, 20.7460, 19.7300, 18.2760]
                }
cols = ['2 layers', '3 layers', '4 layers', '5 layers', '6 layers']
df = pd.DataFrame(layers_time, columns = cols)

# columns means evaluation
means = df.mean(axis = 0).to_list()

# dataframe reshaping
df = pd.melt(frame = df,
             var_name = 'n layers',
             value_name = 'value')

# figure set-up
fig, ax = plt.subplots()

# mean plot
ax.plot(cols, means, color = 'black', marker = 'o', linestyle = '')

# dot-plot
sns.stripplot(ax = ax,
              data = df,
              x = 'n layers',
              y = 'value')

# show figure
plt.show()

Output

enter image description here

stripplot/swarmplot comparison

enter image description here

The choice between stripplot or swarmplot depends on the amount of your data: swarmplot prevents dots overlapping, so it is preferable when you don't have an excessive amount of points and allows to appreciate a distribution of your data. On the contrary, stripplot allows dots overlapping, so you lose information regarding data distribution but you can plot a larger amount of points.

Related