Python Numpy + Matplotlib 再探
为什么是再探?因为之前咕了不知道多少次了,看了忘忘了看.
Numpy
ndarray
- NumPy’s array class is called
ndarray
. It is also known by the aliasarray
.
import numpy as np
li = np.arange(20).reshape((4,5))
print(li)
print(li.ndim)
print(li.dtype)
print(li.size)
print(type(li))
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
2
int64
20
<class 'numpy.ndarray'>
arange(lower_bound, upper_bound, step)
,zeros
,ones
,linspace(lo,hi,count)
import math
import numpy as np
x = np.linspace(0, math.pi*2, 10)
y = np.sin(x)
print(x, y)
[0. 0.6981317 1.3962634 2.0943951 2.7925268 3.4906585
4.1887902 4.88692191 5.58505361 6.28318531]
[ 0.00000000e+00 6.42787610e-01 9.84807753e-01 8.66025404e-01
3.42020143e-01 -3.42020143e-01 -8.66025404e-01 -9.84807753e-01
-6.42787610e-01 -2.44929360e-16]
reshape()
Basic operations
import numpy as np
a = np.array([10, 20, 30, 40, 50])
b = np.arange(1, 6)
print(a)
print(b)
print(a+b)
print(a/b)
print(b**2)
print(a < 30)
print(a*b)
print(a.dot(b))
c = np.array([[1, 2], [3, 4]])
d = np.array([[5, 6], [7, 8]])
print(c@d)
[10 20 30 40 50]
[1 2 3 4 5]
[11 22 33 44 55]
[10. 10. 10. 10. 10.]
[ 1 4 9 16 25]
[ True True False False False]
[ 10 40 90 160 250]
550
[[19 22]
[43 50]]
exp()
…
Indexing and Slicing
- One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences.
- Multidimensional arrays can have one index per axis. These indices are given in a tuple separated by commas.
- The dots (
...
) represent as many colons as needed to produce a complete indexing tuple. For example, ifx
is an array with 5 axes, thenx[1, 2, ...]
is equivalent tox[1, 2, :, :, :]
,x[..., 3]
tox[:, :, :, :, 3]
andx[4, ..., 5, :]
tox[4, :, :, 5, :]
.
Iterating
- Iterating over multidimensional arrays is done with respect to the first axis.
- But you can use
.flat
attribute which could serve as an iterator to iterate over all the elements over the array.
Matplotlib
Goals
画不同图像
- 散点图、折线图
- 饼图
- 柱状图
更改表格的 style
- 子图
图像的绘制
散点图与折线图
from matplotlib import pyplot as plt
import numpy as np
import math
x = np.linspace(0, 2*math.pi, 5)
y = np.sin(x)
plt.plot(x, y)
plt.savefig("1.png")
And when we changed 5 points to 500…
Markers and line styles
Markers
character | description |
---|---|
'.' |
point marker |
',' |
pixel marker |
'o' |
circle marker |
'v' |
triangle_down marker |
'^' |
triangle_up marker |
'<' |
triangle_left marker |
'>' |
triangle_right marker |
'1' |
tri_down marker |
'2' |
tri_up marker |
'3' |
tri_left marker |
'4' |
tri_right marker |
'8' |
octagon marker |
's' |
square marker |
'p' |
pentagon marker |
'P' |
plus (filled) marker |
'*' |
star marker |
'h' |
hexagon1 marker |
'H' |
hexagon2 marker |
'+' |
plus marker |
'x' |
x marker |
'X' |
x (filled) marker |
'D' |
diamond marker |
'd' |
thin_diamond marker |
![]() |
vline marker |
'_' |
hline marker |
- Example
from matplotlib import pyplot as plt
import numpy as np
import math
x = np.linspace(0, 2*math.pi, 8)
y = np.sin(x)
plt.plot(x, y, 's')
plt.savefig("1.png")
Line styles
character | description |
---|---|
'-' |
solid line style |
'--' |
dashed line style |
'-.' |
dash-dot line style |
':' |
dotted line style |
- Example
from matplotlib import pyplot as plt
import numpy as np
import math
x = np.linspace(0, 2*math.pi, 20)
y = np.sin(x)
plt.plot(x, y, 's-')
plt.savefig("1.png")
饼图
pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=0, radius=1, counterclock=True, wedgeprops=None, textprops=None, center=0, 0, frame=False, rotatelabels=False, *, normalize=None, data=None)[source]
from matplotlib import pyplot as plt
import numpy as np
import math
x = ["CA", "LA", "FOP", "DM"]
y = [90, 90, 60, 50]
plt.pie(y, labels=x)
plt.savefig("1.png")
参数的使用
from matplotlib import pyplot as plt
import numpy as np
import math
x = ["CA", "LA", "FOP", "DM"]
y = [80, 100, 60, 50]
plt.pie(y, labels=x, explode=(0, 0.2, 0, 0), autopct='%.2f%%')
# %d%% 整数百分比,%0.1f%% 一位小数百分比, %0.2f%% 两位小数百分比
plt.savefig("1.png")
柱形图
matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
Prettify
设置标题
plt.title(title)
设置图例
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html#matplotlib.pyplot.legend
Subplots
plt.subplot(nrows, ncols, index)
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplot.html#matplotlib.pyplot.subplot