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.

3
  • 1
    pass rotation='45' in ax.text() method inside your for loop Commented Jul 31, 2021 at 5:12
  • 1
    rotation=45 (no quotes) Commented Jul 31, 2021 at 5:15
  • Have you tried it by passing rotation parameter in ax.text()?.....yes it is accepted either you pass value as an integer or as a string it won't affect the result Commented Jul 31, 2021 at 5:16

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

ax.text(point['x'],point['y'], str(point['lab']), rotation=45)
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.