Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
João C. P. da Silva
Carla A. D. M. Delgado
Dept. Ciência da Computação - UFRJ
Formatos - Cores
Exercı́cio:
Teste outras cores e formatos para o exemplo da função seno. Altere
também o número de pontos.
String Código
‘best’ 0
‘upper right’ 1
‘upper left’ 2
‘lower left’ 3
‘lower,right’ 4
‘right’ 5
‘center left’ 6
‘center right’ 7
‘lower center’ 8
‘upper,center’ 9
‘center’ 10
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computação II - Python Aula 8 13 / 44
Matplotlib - Básico - Rótulos e Legendas
1 f i g , ax = p l t . s u b p l o t s ( f i g s i z e =(10 , 5) )
2
3 x = np . l i n s p a c e ( 0 , 1 0 , 200)
4 y = np . s i n ( x )
5
6 ax . p l o t ( x , y , ’ r−’ , l i n e w i d t h =2, l a b e l =r ’ $y=\ s i n ( x ) $ ’ , a l p h a =0. 6) # mudamos a q u i
7
8 ax . l e g e n d ( l o c= ’ u p p e r c e n t e r ’ )
9
10 p l t . show ( )
Exercı́cios:
Crie um array com números do intervalo [0., 5.) (step = 0.2), e plote
as funções
f (x) = x (formato: traços vermelhos)
g (x) = x 2 (quadrados azuis)
h(x) = x 3 (triângulos verdes).
Fazer duas versões, uma com eles separados e outra em um só gráfico.
Inclua a legenda para as funções e salve as imagens em arquivos.
Exercı́cio
Considerando que você está usando 100 pontos no intervalo [−10, 10],
plote as funções seno e coseno para gerar o seguinte gráfico:
Exercı́cio
Plote o gráfico abaixo. Use 100 pontos e o tamanho da figura de 10 x 8. Salve seu gráfico em arquivo.
1 i m p o r t numpy a s np
2 import m a t p l o t l i b . p y p l o t as p l t
3
4 f i g , ax = p l t . s u b p l o t s ( )
5 x = np . l i n s p a c e ( 0 , 1 , 1 0 0 )
6 y = np . z e r o s ( 1 0 0 )
7
8 y [ x < 0.5] = x [ x < 0.5]
9 y [ x >= 0 . 5 ] = 1 + x [ x >= 0 . 5 ]
10
11 pos = np . whe re ( np . a b s ( np . d i f f ( y ) ) >= 0 . 5 ) [ 0 ] # l i n h a nova
12
13 x [ pos ] = np . nan # l i n h a nova
14 y [ pos ] = np . nan # l i n h a nova
15
16 ax . p l o t ( x , y , ’−o ’ )
17 p l t . s a v e f i g ( ’ /home/ j o a o / De sk top / d e s c o n t 3 . png ’ )
1 i m p o r t numpy a s np
2 import m a t p l o t l i b . p y p l o t as p l t
3
4 f i g , ax = p l t . s u b p l o t s ( )
5 x = np . l i n s p a c e ( 0 , 1 , 1 0 0 )
6 y = np . z e r o s ( 1 0 0 )
7
8 y [ x < 0.5] = x [ x < 0.5]
9 y [ x >= 0 . 5 ] = 1 + x [ x >= 0 . 5 ]
10
11 pos = np . whe re ( np . a b s ( np . d i f f ( y ) ) >= 0 . 5 ) [ 0 ] # l i n h a nova
12
13 x [ pos ] = np . nan # l i n h a nova
14 y [ pos ] = np . nan # l i n h a nova
15
16 ax . p l o t ( x , y , ’−o ’ )
17 p l t . s a v e f i g ( ’ /home/ j o a o / De sk top / d e s c o n t 3 . png ’ )
Exercı́cio: Faça uma função que gere o seguinte gráfico (use 200 pontos e
valores de x ∈ [−10, 10]):
interpolação (scipy.interpolate)
otimização (scipy.optimize)
estatı́stica (scipy.stats)
outras
procedimentos de estimativa
testes estatı́sticos
outros
https://docs.scipy.org/doc/numpy-
1.13.0/reference/generated/numpy.random.beta.html
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computação II - Python Aula 8 28 / 44
Scipy - Estatı́stica - scipy.stats
Ou a função do Scipy.
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.beta.html#scipy.stats.beta
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.beta.html#scipy.stats.beta
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computação II - Python Aula 8 30 / 44
Scipy - Estatı́stica - scipy.stats
norm: variável aleatória contı́nua normal
Função de Densidade da Distribuição Normal
1 x−µ )2
e− 2 ( σ
f (x) = √
σ 2π
onde µ é a média e σ o desvio padrão.
No scipy, ela é definida para a µ = 1 (parâmetro loc) e σ = 0 (parâmetro scale) por
default:
x2
e− 2
f (x) = √
2π
1 import m a t p l o t l i b . p y p l o t as p l t
2 i m p o r t numpy a s np
3 from s c i p y . s t a t s i m p o r t norm # v a r i a v e l a l e a t o r i a c onti nua normal
4
5
6 f i g , ax = p l t . s u b p l o t s ( )
7
8 x = np . l i n s p a c e ( −4, 4 , 150)
9 y = norm . p d f ( x )
10
11 ax . p l o t ( x , y , l i n e w i d t h =2)
12
13 p l t . s a v e f i g ( ’ norm al 1 . png ’ )
No scipy, o intervalo [a, b] é definido por [loc, loc + scale], onde por default loc = 0 e
scale = 1:
1 import m a t p l o t l i b . p y p l o t as p l t
2 i m p o r t numpy a s np
3 from s c i p y . s t a t s i m p o r t u n i f o r m # v a r i a v e l a l e a t o r i a continua uniforme
4
5
6 f i g , ax = p l t . s u b p l o t s ( )
7
8 x = np . l i n s p a c e ( −4, 4 , 150)
9 y = uniform . pdf ( x ) # de nsidade uniforme
10
11 ax . p l o t ( x , y , l i n e w i d t h =2)
12
13 p l t . s a v e f i g ( ’ u n i f o r m e 1 . png ’ )
Quantecon: https://lectures.quantecon.org/py/matplotlib.html#the-
matlab-style-api
João C. P. da Silva
Carla A. D. M. Delgado
Dept. Ciência da Computação - UFRJ