py lecture5 python plots
TRANSCRIPT
python 2D plotting library which produces publication quality figures in
› a variety of hardcopy formats
› interactive environments
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()
Draw the graph(y = x^2).
x = range(100)
y = [i * i for i in x]
plt.plot(x, y, 'r-')
plt.ylabel('y = x^2')
plt.show()
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()
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()
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()
For more properties, do
› lines=plt.plot([1, 2, 3])
› plt.setp(lines)
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()
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()
Draw the graph of binary entropy
function 𝐻 𝑝 = −𝑝log2 𝑝 − 1 − 𝑝 log2 1 − 𝑝
› Hint: use np.log2 to
calculate log2
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()
Matplotlib tutorial
http://matplotlib.org/users/pyplot_tutorial.html
りんご大学 - 品種別生産量の推移
http://www.ringodaigaku.com/study/statistics/production_kind.html