HCSthe2nd
HCSthe2nd

Reputation: 185

Adding labels to points in seaborn stripplot

I'm using stripplot on seaborn to show energies of d orbitals in a series of metallic centers.

Here is the dataframe:

        dxy      dyz      dz2      dxz   dx2-y2
Fe -0.25336 -0.24661 -0.22991 -0.07644 -0.16229
Co -0.38294 -0.38050 -0.34952 -0.21271 -0.27173
Ni -0.47550 -0.47504 -0.46817 -0.44385 -0.45632

And using this code I'm quite close to what I want (resulting image below):

plt.figure(figsize=(3, 7))
sns.stripplot(x=df.index, y="dxy", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
sns.stripplot(x=df.index, y="dyz", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
sns.stripplot(x=df.index, y="dz2", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
sns.stripplot(x=df.index, y="dxz", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
sns.stripplot(x=df.index, y="dx2-y2", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
plt.ylabel("Energy (Eh)")
plt.savefig('svm_conf.png', dpi=400)

I'd like to add the name of each orbital (dxy, dxz etc), as a label, to the right (or the best possible position) on each point (or line in this case haha).

Any help is much appreciated.

P.S.: I can see that the figure generated at the end is missing part of the numbers in the Y axis. Why?

Update

I'm giving a try to a seaborn version of the solution proposed by @ImportanceOfBeingErnest. Testing with only one of the markers, for now, I'm getting this AttributeError: 'NoneType' object has no attribute 'update' with a long traceback that doesn't make much sense to me. Here is my code:

dxy = sns.stripplot(x=df.index, y="dxy", data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2)
for line in range(0, df.shape[0]):
    dxy.text(df.index[line], df.dxy[line], "teste", horizontalalignment='right', size='medium', color='black')

enter image description here

Upvotes: 3

Views: 5753

Answers (1)

Nihal
Nihal

Reputation: 5324

    g.text(x=i+0.1, y=df[df.columns[j]].values[i]+0.001, s=df.columns[j], horizontalalignment='right', size='medium', color='black')

upto you how you want to add or subtract numbers in (x,y) coordinates

data:(sam.csv)

d,dxy,dyz,dz2,dxz,dx2-y2
Fe,-0.25336,-0.24661,-0.22991,-0.07644,-0.16229
Co,-0.38294,-0.38050,-0.34952,-0.21271,-0.27173
Ni,-0.47550,-0.47504,-0.46817,-0.44385,-0.45632

code:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib.lines import Line2D
import random

df = pd.read_csv('sam.csv').reset_index()
df.index = df['d']
del df['d']
print(df.columns)
print(df)
ax = plt.figure(figsize=(10, 7))
colors = ["#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)])
             for i in range(len(df.columns) - 1)]
# colors = ['#FFAA11', '#11AA11', '#55AA31', '#11BA81', '#CCABAA']

columns = list(df.columns)
for j in range(1, len(df.columns)):
    color = colors[j-1]
    g = sns.stripplot(x=df.index, y=columns[j], data=df, jitter=False, dodge=True, size=44, marker="_", linewidth=2,
                      color=color)

for j in range(1, len(df.columns)):

    for i in range(len(df)):
        g.text(x=i+0.1, y=df[df.columns[j]].values[i]+0.001, s=df.columns[j], horizontalalignment='right', size='medium', color='black')

elements = [Line2D([0], [0], color=colors[i]) for i in range(len(df.columns)-1)]

ax.legend(handles=elements, labels=list(df.columns)[1:])

plt.ylabel("Energy (Eh)")
plt.savefig('svm_conf.png', dpi=400)
plt.show()

output:

Index(['index', 'dxy', 'dyz', 'dz2', 'dxz', 'dx2-y2'], dtype='object')

enter image description here


Problem in your code:

In your updated code dxy.text(df.index[line], df.dxy[line], "teste", horizontalalignment='right', size='medium', color='black')

here x=df.index[line] which is string, it should be numeric because its a coordinate for your text


second approach:

for j in range(1, len(df.columns)):
    flag = True
    for i in range(len(df)):
        if flag is True:
            delta = 0.3
            align = 'left'
            flag = False
        else:
            delta = -0.2
            align = 'right'
            flag = True
        g.text(x=i+delta, y=df[df.columns[j]].values[i], s=df.columns[j], horizontalalignment=align, size='medium', color='black')

output:

enter image description here

Upvotes: 2

Related Questions