Matplotlib Tutorial - Título Eixos
Neste tutorial vamos aprender sobre o título do eixo em Matplotlib.
Título Eixos Matplotlib
Sintaxe:
matplotlib.pyplot.title(label, fontdict=None, loc=None, **kwargs)
Define um título dos eixos actuais.
Parâmetros*
Nome | Tipo de dados | Descrição |
---|---|---|
label |
str |
texto do rótulo |
fontdict |
dict |
dicionário de texto do rótulo, como família, cor, peso e tamanho |
loc |
str |
A localização do título. Tem três opções, {'center', 'left', 'right'} e a opção padrão é center . |
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4 * np.pi, 1000)
y = np.sin(x)
plt.figure(figsize=(4, 3))
plt.plot(x, y, "r")
plt.xlabel("Time (s)", size = 16,)
plt.ylabel("Value", size = 16)
plt.title("Title Example",
fontdict={'family': 'serif',
'color' : 'darkblue',
'weight': 'bold',
'size': 18})
plt.grid(True)
plt.show()
plt.title("Title Example",
fontdict={'family': 'serif',
'color' : 'darkblue',
'weight': 'bold',
'size': 18})
Eixo Matplotlib Títulos Múltiplos
Um eixo pode ter no máximo três títulos que estão nas posições left
, center
e right
. A posição do título específico é especificada com o argumento loc
.
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4 * np.pi, 1000)
y = np.sin(x)
plt.figure(figsize=(8, 6))
plt.plot(x, y, "r")
plt.xlabel("Time (s)", size = 16,)
plt.ylabel("Value", size = 16)
plt.title("Left title",
fontdict={'family': 'serif',
'color' : 'darkblue',
'weight': 'bold',
'size': 16},
loc='left')
plt.title("Center title",
fontdict={'family': 'monospace',
'color' : 'red',
'weight': 'bold',
'size': 16},
loc='center')
plt.title("Right title",
fontdict={'family': 'fantasy',
'color' : 'black',
'weight': 'bold',
'size': 16},
loc='right')
plt.grid(True)
plt.show()
Eixo Matplotlib Título dentro do Lote
Você também poderia colocar o título dentro da trama com a opção positon=(m, n)
ou equivalente x = m, y = n
. Aqui, m
e n
são números entre 0.0 e 1.0.
A posição (0, 0)
é o canto inferior esquerdo do gráfico, e a posição (1.0, 1.0)
é o canto superior direito.
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4 * np.pi, 1000)
y = np.sin(x)
plt.figure(figsize=(6, 4.5))
plt.plot(x, y, "r")
plt.xlabel("Time (s)", size = 16)
plt.ylabel("Value", size = 16)
plt.title("Title Example",
position=(0.5, 0.9),
fontdict={'family': 'serif',
'color' : 'darkblue',
'weight': 'bold',
'size': 16})
plt.show()