py lecture5 python plots

20

Upload: yoshiki-satotani

Post on 08-Aug-2015

34 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Py lecture5 python plots
Page 2: Py lecture5 python plots

python 2D plotting library which produces publication quality figures in

› a variety of hardcopy formats

› interactive environments

Page 3: Py lecture5 python plots

import matplotlib.pyplot as plt

# x data, y data, style

plt.plot([1,2,3,4], [5,6,7,8], 'r-')

# [x min, x max, y min, y max]

plt.axis([0, 5, 0, 9])

plt.ylabel('some numbers')

plt.show()

Page 4: Py lecture5 python plots
Page 5: Py lecture5 python plots

Draw the graph(y = x^2).

Page 6: Py lecture5 python plots

x = range(100)

y = [i * i for i in x]

plt.plot(x, y, 'r-')

plt.ylabel('y = x^2')

plt.show()

Page 7: Py lecture5 python plots

import matplotlib.pyplot as plt

data = [2, 7, 6, 4, 1, 10, 3, 2, 4, 5, 3, 1]

plt.hist(data, bins=8, facecolor='blue')

plt.show()

Page 8: Py lecture5 python plots
Page 9: Py lecture5 python plots

import matplotlib.pyplot as plt

# The slices will be ordered and plotted counter-clockwise. labels = 'Fuji', 'Tsugaru', 'Orin', 'Jonagold', 'Other' sizes = [235500, 50600, 47100, 45700, 89100] colors = ['orangered', 'red', 'greenyellow', 'orangered', 'gold'] explode = (0, 0, 0.1, 0, 0) # only "explode" the 3rd slice (i.e. 'Orin')

plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90, counterclock=False) # Set aspect ratio to be equal so that pie is drawn as a circle. plt.axis('equal')

plt.show()

Page 10: Py lecture5 python plots
Page 11: Py lecture5 python plots

import numpy as np # use numpy

import matplotlib.pyplot as plt

x = np.array(range(10))

# red line

plt.plot(x, x, ls='-', c='red')

# blue circles

plt.plot(x, x**1.25, ls='', c='blue', marker='o')

# green stars

plt.plot(x, x**1.5, ls='', c='green', marker='*')

plt.show()

Page 12: Py lecture5 python plots
Page 13: Py lecture5 python plots

For more properties, do

› lines=plt.plot([1, 2, 3])

› plt.setp(lines)

Page 14: Py lecture5 python plots

import numpy as np

import matplotlib.pyplot as plt

x = np.array(range(10))

# first figure

plt.figure(1)

# num of row, num of column, num of axis

plt.subplot(2, 3, 1)

plt.plot(x, x, 'r')

plt.subplot(2, 3, 2)

plt.plot(x, x, 'bo')

plt.show()

Page 15: Py lecture5 python plots
Page 16: Py lecture5 python plots

import numpy as np import matplotlib.pyplot as plt

x = np.arange(0, 1, 0.001) y = x**1.5 plt.plot(x, y, 'r')

plt.xlabel(r'$x$') plt.ylabel(r'$y=x^{1.5}$') plt.title(r'Graph of $y=x^{1.5}$') plt.text(0.3, 0.25, r'$y=x^{1.5}$') plt.grid(True)

plt.show()

Page 17: Py lecture5 python plots
Page 18: Py lecture5 python plots

Draw the graph of binary entropy

function 𝐻 𝑝 = −𝑝log2 𝑝 − 1 − 𝑝 log2 1 − 𝑝

› Hint: use np.log2 to

calculate log2

Page 19: Py lecture5 python plots

import numpy as np import matplotlib.pyplot as plt

p = np.arange(0, 1, 0.001) H = -p*np.log2(p) - (1-p)*np.log2(1-p)

plt.plot(p, H, 'r') plt.xlabel(r'$p$') plt.ylabel(r'$H(p)=-p ¥log_{2}(p)-(1-p)¥log_{2}(1-p)$') plt.title('Binary Entropy Function')

plt.grid(True) plt.show()

Page 20: Py lecture5 python plots

Matplotlib tutorial

http://matplotlib.org/users/pyplot_tutorial.html

りんご大学 - 品種別生産量の推移

http://www.ringodaigaku.com/study/statistics/production_kind.html