0

I'd like to rotate text / labels for each (x,y) scatterplot points.

Here's my dataframe:

import pandas as pd
import seaborn sns 

df1 = pd.DataFrame({"one": [1,2,3,5,6,7,8],
                   "two": [3,6,4,3,8,2,5],
                   "label":['a','b','c','d','e','f','g']})

ax = sns.scatterplot(x='one', y='two', data = df1)

for line in range(0,df1.shape[0]):
    ax.text(df1.one[line]+0.04, 
            df1.two[line], 
            df1.label[line], horizontalalignment='left', 
            size='medium', color='black', weight='semibold')

enter image description here

I'd like to rotate labels to 45, so that they're visible / clear.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
kms
  • 1,810
  • 1
  • 41
  • 92

2 Answers2

3

use this code for rotate the label on graph

df1 = pd.DataFrame({"one": [1,2,3,5,6,7,8],
                   "two": [3,6,4,3,8,2,5],
                   "label":['a','b','c','d','e','f','g']})
ax=sns.scatterplot(x='one', y='two', data = df1)

a=pd.concat({"x":df1["one"],"y":df1["two"],"lab":df1["label"]},axis=1)
for i ,point in a.iterrows():
    ax.text(point['x'],point['y'],str(point['lab'])).set_rotation(45)

to rotate the label in graph

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
1

The following would do it:

plt.figure(figsize=(10,10))
p1 = sns.scatterplot(x='one', y='two', data=df1, legend=False)  

for i in range(0,df1.shape[0]):
    p1.text(df1.one[i]+0.04, df1.two[i], 
    df1.label[i], horizontalalignment='left', 
    size='medium', color='black', weight='semibold')

plt.title('Example Plot')
plt.xlabel('One')
plt.ylabel('Two')

enter image description here

Ruthger Righart
  • 4,799
  • 2
  • 28
  • 33