본문 바로가기

Python/Matplotlib

[Python] matplotlib - plot outside text

반응형

plot 바깥쪽에 text 쓰기

plt.text(1.03, 0.95, textstr, transform=ax.transAxes, fontsize=14, verticalalignment='top', bbox=props)

 

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(19680801)

fig, ax = plt.subplots()
x = 30*np.random.randn(10000)
mu = x.mean()
median = np.median(x)
sigma = x.std()
textstr = '\n'.join((
    r'$\mu=%.2f$' % (mu, ),
    r'$\mathrm{median}=%.2f$' % (median, ),
    r'$\sigma=%.2f$' % (sigma, )))

ax.hist(x, 50)
# these are matplotlib.patch.Patch properties
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)

plt.text(1.03, 0.95, textstr, transform=ax.transAxes, fontsize=14, verticalalignment='top', bbox=props)

plt.show()

예제는 upper left 위치

https://matplotlib.org/3.1.0/gallery/recipes/placing_text_boxes.html

 

Placing text boxes — Matplotlib 3.1.0 documentation

Placing text boxes When decorating axes with text boxes, two useful tricks are to place the text in axes coordinates (see Transformations Tutorial), so the text doesn't move around with changes in x or y limits. You can also use the bbox property of text t

matplotlib.org

 

반응형