The answers by tmdavison only support the ps
format. So, I tried to solve the problem to accept any format. To show how I solve this problem, I provide a dataset that makes the whole idea sample to understand. In my case, I have more than 34 figures with different points, and every point needs to be described. I use this code, and I added the x=min(x-axis)
and y=median(y-axis)
.
Here is the content of the emp.csv

import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.transforms import Affine2D
import os
cwd = os.path.dirname(__file__)
def rainbow_text(x, y, strings, colors, orientation='vertical',
ax=None, **kwargs):
if ax is None:
ax = plt.gca()
t = ax.transData
canvas = ax.figure.canvas
assert orientation in ['horizontal', 'vertical']
if orientation == 'vertical':
kwargs.update(rotation=90, verticalalignment='bottom')
for s, c in zip(strings, colors):
text = ax.text(x, y, s + " ", color=c, transform=t, **kwargs)
# Need to draw to update the text position.
text.draw(canvas.get_renderer())
ex = text.get_window_extent()
if orientation == 'horizontal':
t = text.get_transform() + Affine2D().translate(ex.width, 0)
else:
t = text.get_transform() + Affine2D().translate(0, ex.height)
df = pd.read_csv('emp.csv', error_bad_lines=False)
colors_ = ['black', 'red', 'black', 'red']
i=0
for row in df.itertuples(index=True, name='Pandas'):
plt.scatter(getattr(row, "sal"), getattr(row, "inc"), color = 'b', s=10)
word = ['First name=', str(getattr(row, "first_name")), 'Last name=', str(getattr(row, "last_name"))]
rainbow_text(df["sal"].min()-8.3, df["inc"].median()-1.2, word, colors_, size=5)
word = ['age=', str(getattr(row, "age")), 'gender=', str(getattr(row, "gender"))]
rainbow_text(df["sal"].min()-7.8, df["inc"].median()-1.2, word, colors_, size=5)
plt.savefig(os.path.join(cwd, 'fig/Test_fig_' + str(i) + '.pdf'), format='pdf', dpi=600, bbox_inches='tight')
i += 1
plt.show()
