A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.python-graph-gallery.com/46-add-text-annotation-on-scatterplot/ below:

Website Navigation


Add text annotation on scatterplot

You can create a basic scatterplot using regplot() function of seaborn library. The following parameters should be provided:

import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns
 
# Create dataframe
df = pd.DataFrame({
'x': [1, 1.5, 3, 4, 5],
'y': [5, 15, 5, 10, 2],
'group': ['A','other group','B','C','D']
})
 
sns.scatterplot(data=df, x="x", y="y", s=200)

plt.show()

Once you have created the dataset and plotted the scatterplot with the previous code, you can use text() function of matplotlib to add annotation. The following parameters should be provided:

You can also specify the additional parameters such as horizontalalignment, size, color, weight to design your text.

import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns
 
# Create dataframe
df = pd.DataFrame({
'x': [1, 1.5, 3, 4, 5],
'y': [5, 15, 5, 10, 2],
'group': ['A','other group','B','C','D']
})
 
sns.scatterplot(data=df, x="x", y="y", s=200)
plt.text(x=1.7, y=15, s="A", weight="bold")

plt.show()

Use a loop to annotate each marker

If you want to annotate every markers, it is practical to use a loop as follow:

import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns
 
# Create dataframe
df = pd.DataFrame({
'x': [1, 1.5, 3, 4, 5],
'y': [5, 15, 5, 10, 2],
'group': ['A','other group','B','C','D']
})
 
sns.scatterplot(data=df, x="x", y="y", s=200)

# add annotations one by one with a loop
for line in range(0,df.shape[0]):
     plt.text(
          df["x"][line]+0.2,
          df["y"][line],
          df["group"][line],
          ha='left',
          weight='bold'
     )

plt.show()

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4