Dot plot with column names on x-axis and shape of dots by index names

Question:

I have this toy dataframe

data = {'Column 1' : [1., 2., 3., 4.],
        'Column 2' : [1.2, 2.2, 3.2, 4.2]
       }
df = pd.DataFrame(data, index=["Apples", "Oranges", "Puppies", "Ducks"])

How can I make a dot/scatter plot of the dataframe with column names on the x-axis and the shape of the dots are different based on the index values, something similar to this?

enter image description here

Asked By: Nemo

||

Answers:

You could use seaborn’s scatterplot which supports marker styles out of the box. Seaborn works easiest with data in "long form", which can be created via pd.melt().

Here is an example:

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

data = {'Column 1': [1., 2., 3., 4.],
        'Column 2': [1.2, 2.2, 3.2, 4.2]}
df = pd.DataFrame(data, index=["Apples", "Oranges", "Puppies", "Ducks"])
df.index.name = 'Item'  # give the index a name, to be used as column name by reset_index()
df_long = df.reset_index().melt(id_vars='Item')

marker_styles = {"Apples": "^", "Oranges": "X", "Puppies": "*", "Ducks": "v"}

ax = sns.scatterplot(data=df_long, x='variable', y='value',
                     style='Item', markers=marker_styles, hue='Item', palette='dark')
sns.move_legend(ax, loc='upper left', bbox_to_anchor=(1.01, 1.01))
ax.set(xlabel='', ylabel='')
plt.tight_layout()
plt.show()

sns.scatterplot with marker styles

Answered By: JohanC