Python基础(十二) 还不会python绘图?两万字博文教你Matplotlib库(超详细总结)_python绘图不从头开始

157 阅读13分钟

plt.tick_params(axis="both", labelsize=15)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/c569458799d6424486ee80da7cb96ede~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=0pPnDkXDxpWDWbn%2BZ9rf%2Bj35bNc%3D)


##### 【3】设置图形标签



x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x)) plt.title("A Sine Curve", fontsize=20) plt.xlabel("x", fontsize=15) plt.ylabel("sin(x)", fontsize=15)



Text(0, 0.5, 'sin(x)')


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/4c50d636676e4eecac9c5071f11fa472~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=0u2PBbMu48avlh4n%2Bw1n8Mq9fbU%3D)


【4】设置图例


* 默认



x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x), "b-", label="Sin") plt.plot(x, np.cos(x), "r--", label="Cos") plt.legend()



<matplotlib.legend.Legend at 0x1884749f908>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/b9dd6490577b4aa4b87262ff997f414f~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=ofXwWJ3dEHDMuGuGT2gvpIvCzc4%3D)


* 修饰图例



import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x), "b-", label="Sin") plt.plot(x, np.cos(x), "r--", label="Cos") plt.ylim(-1.5, 2) plt.legend(loc="upper center", frameon=True, fontsize=15) # frameon=True增加图例的边框



<matplotlib.legend.Legend at 0x19126b53b80>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/e2c592bf7aa840c881326da97c42c604~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=eNEc8bTMllpthUD4KuHhP%2FuIR00%3D)


【5】添加文字和箭头


* 添加文字



x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x), "b-") plt.text(3.5, 0.5, "y=sin(x)", fontsize=15) # 前两个为文字的坐标,后面是内容和字号



Text(3.5, 0.5, 'y=sin(x)')


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/04af95851dab4a63b2b5ec55d00ac6cf~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=PoZZNEO2LYX3S1Ufo9pQHzkKZcA%3D)


* 添加箭头



x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x), "b-") plt.annotate('local min', xy=(1.5*np.pi, -1), xytext=(4.5, 0), arrowprops=dict(facecolor='black', shrink=0.1), )



Text(4.5, 0, 'local min')


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/46d8f174fea74f7d84c9d2270548ff9a~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=q7%2FkWSPac%2BgylR5cpkIn%2F%2FLxjI4%3D)


#### 13.1.2 散点图1】简单散点图



x = np.linspace(0, 2*np.pi, 20) plt.scatter(x, np.sin(x), marker="o", s=30, c="r") # s 大小 c 颜色



<matplotlib.collections.PathCollection at 0x188461eb4a8>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/8441e194df31456080ca99fa47de0285~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=kxGwmeO4VGtNr2qjh%2F8qRTGaJFo%3D)


【2】颜色配置



x = np.linspace(0, 10, 100) y = x**2 plt.scatter(x, y, c=y, cmap="inferno") # 让c随着y的值变化在cmap中进行映射 plt.colorbar() # 输出颜色条



<matplotlib.colorbar.Colorbar at 0x18848d392e8>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/59980758b5a34c2b9a749a44460ce188~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=1qx1r22bbBZ3HDQoX6O36a1troI%3D)


颜色配置参考官方文档


https://matplotlib.org/examples/color/colormaps\_reference.html3】根据数据控制点的大小



x, y, colors, size = (np.random.rand(100) for i in range(4)) plt.scatter(x, y, c=colors, s=1000*size, cmap="viridis")



<matplotlib.collections.PathCollection at 0x18847b48748>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/7b0ae0e8750e40509a9d70beba6bd31f~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=ueGMzs4QGPwFAsJ7KDVTgQGBJoU%3D)


【4】透明度



x, y, colors, size = (np.random.rand(100) for i in range(4)) plt.scatter(x, y, c=colors, s=1000*size, cmap="viridis", alpha=0.3) plt.colorbar()



<matplotlib.colorbar.Colorbar at 0x18848f2be10>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/1f992351c25c4a42861aacae19593c2d~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=Aw8aeo4Q6GOw1nFd3UVIvlQWYwA%3D)


【例】随机漫步



from random import choice

class RandomWalk(): """一个生产随机漫步的类""" def __init__(self, num_points=5000): self.num_points = num_points self.x_values = [0] self.y_values = [0]

def fill\_walk(self):
    while len(self.x_values) < self.num_points:
        x_direction = choice([1, -1])
        x_distance = choice([0, 1, 2, 3, 4])
        x_step = x_direction \* x_distance
        
        y_direction = choice([1, -1])
        y_distance = choice([0, 1, 2, 3, 4])
        y_step = y_direction \* y_distance            
    
        if x_step == 0 or y_step == 0:
            continue
        next_x = self.x_values[-1] + x_step
        next_y = self.y_values[-1] + y_step
        self.x_values.append(next_x)
        self.y_values.append(next_y)


rw = RandomWalk(10000) rw.fill_walk() point_numbers = list(range(rw.num_points)) plt.figure(figsize=(12, 6)) # 设置画布大小 plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap="inferno", s=1) plt.colorbar() plt.scatter(0, 0, c="green", s=100) plt.scatter(rw.x_values[-1], rw.y_values[-1], c="red", s=100)

plt.xticks([]) plt.yticks([])



([], <a list of 0 Text yticklabel objects>)


​  
 ![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/b69a111efb4e4e809b94861384bb7c47~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=aD8IhpZcXjsMtKReXUaYFJuDYdI%3D)  
 ​


#### 13.1.3 柱形图


【1】简单柱形图



x = np.arange(1, 6) plt.bar(x, 2*x, align="center", width=0.5, alpha=0.5, color='yellow', edgecolor='red') plt.tick_params(axis="both", labelsize=13)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/5fe364a186b44ce4aa7ddd6fd97b19ec~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=VIfsXUmZqg8WTjuhb2o%2BY%2BG%2BWtE%3D)



x = np.arange(1, 6) plt.bar(x, 2*x, align="center", width=0.5, alpha=0.5, color='yellow', edgecolor='red') plt.xticks(x, ('G1', 'G2', 'G3', 'G4', 'G5')) plt.tick_params(axis="both", labelsize=13)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/59a4b849ec5c43a9982aa7b5cc770a27~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=5HchwBG%2BMKjmM%2Ff1aFc2lZwHB8k%3D)



x = ('G1', 'G2', 'G3', 'G4', 'G5') y = 2 * np.arange(1, 6) plt.bar(x, y, align="center", width=0.5, alpha=0.5, color='yellow', edgecolor='red') plt.tick_params(axis="both", labelsize=13)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/556f7a280d75438e8477a907b7d47619~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=hf0Ed7olg4JvVnXml11ONAwgBRE%3D)



x = ["G"+str(i) for i in range(5)] y = 1/(1+np.exp(-np.arange(5)))

colors = ['red', 'yellow', 'blue', 'green', 'gray'] plt.bar(x, y, align="center", width=0.5, alpha=0.5, color=colors) plt.tick_params(axis="both", labelsize=13)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/8630ce0e41414935a449a32198948039~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=j9d0%2B%2Blq6OuxvyUTvCp3HJzi4u8%3D)


【2】累加柱形图



x = np.arange(5) y1 = np.random.randint(20, 30, size=5) y2 = np.random.randint(20, 30, size=5) plt.bar(x, y1, width=0.5, label="man") plt.bar(x, y2, width=0.5, bottom=y1, label="women") plt.legend()



<matplotlib.legend.Legend at 0x2052db25cc0>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/4d573eb9a29e444684974adc02039b2f~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=v8QVlf%2F21i%2F9x82Bsnhu44wpYVE%3D)


【3】并列柱形图



x = np.arange(15) y1 = x+1 y2 = y1+np.random.random(15) plt.bar(x, y1, width=0.3, label="man") plt.bar(x+0.3, y2, width=0.3, label="women") plt.legend()



<matplotlib.legend.Legend at 0x2052daf35f8>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/df2fc2091275463b936434028bba0c53~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=JeyHrNn3%2BxBcEqdPrlY15Zvh%2Bfk%3D)


【4】横向柱形图barh



x = ['G1', 'G2', 'G3', 'G4', 'G5'] y = 2 * np.arange(1, 6) plt.barh(x, y, align="center", height=0.5, alpha=0.8, color="blue", edgecolor="red") # 注意这里将bar改为barh,宽度用height设置 plt.tick_params(axis="both", labelsize=13)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/583c366459cc4e3a91b2a4cf87c2cd4e~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=L1gRj85MTJJ4qQI0TdhVXjah%2B6U%3D)


#### 13.1.4 多子图


【1】简单多子图



def f(t): return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02)

plt.subplot(211) plt.plot(t1, f(t1), "bo-", markerfacecolor="r", markersize=5) plt.title("A tale of 2 subplots") plt.ylabel("Damped oscillation")

plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), "r--") plt.xlabel("time (s)") plt.ylabel("Undamped")



Text(0, 0.5, 'Undamped')


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/ed9240ad57ee41b98897ebc32726ab73~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=%2FUWeV0xpnF%2BxH34NrKP73O10J28%3D)


【2】多行多列子图



x = np.random.random(10) y = np.random.random(10)

plt.subplots_adjust(hspace=0.5, wspace=0.3)

plt.subplot(321) plt.scatter(x, y, s=80, c="b", marker=">")

plt.subplot(322) plt.scatter(x, y, s=80, c="g", marker="*")

plt.subplot(323) plt.scatter(x, y, s=80, c="r", marker="s")

plt.subplot(324) plt.scatter(x, y, s=80, c="c", marker="p")

plt.subplot(325) plt.scatter(x, y, s=80, c="m", marker="+")

plt.subplot(326) plt.scatter(x, y, s=80, c="y", marker="H")



<matplotlib.collections.PathCollection at 0x2052d9f63c8>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/ac90d6d43fee4490ad0480410f1cf370~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=NS1epSDc3RAPCJoRIq0deWORtiM%3D)


【3】不规则多子图



def f(x): return np.exp(-x) * np.cos(2*np.pi*x)

x = np.arange(0.0, 3.0, 0.01) grid = plt.GridSpec(2, 3, wspace=0.4, hspace=0.3) # 两行三列的网格

plt.subplot(grid[0, 0]) # 第一行第一列位置 plt.plot(x, f(x))

plt.subplot(grid[0, 1:]) # 第一行后两列的位置 plt.plot(x, f(x), "r--", lw=2)

plt.subplot(grid[1, :]) # 第二行所有位置 plt.plot(x, f(x), "g-.", lw=3)



[<matplotlib.lines.Line2D at 0x2052d6fae80>]


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/fb9d206105ef4d31af29a5df48c5cd0d~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=QLITPNc9HxLNkixMCKBjY%2FQAmYw%3D)


#### 13.1.5 直方图


【1】普通频次直方图



mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000)

plt.hist(x, bins=50, facecolor='g', alpha=0.75)



(array([ 1., 0., 0., 5., 3., 5., 1., 10., 15., 19., 37., 55., 81., 94., 125., 164., 216., 258., 320., 342., 401., 474., 483., 590., 553., 551., 611., 567., 515., 558., 470., 457., 402., 347., 261., 227., 206., 153., 128., 93., 79., 41., 22., 17., 21., 9., 2., 8., 1., 2.]), array([ 40.58148736, 42.82962161, 45.07775586, 47.32589011, 49.57402436, 51.82215862, 54.07029287, 56.31842712, 58.56656137, 60.81469562, 63.06282988, 65.31096413, 67.55909838, 69.80723263, 72.05536689, 74.30350114, 76.55163539, 78.79976964, 81.04790389, 83.29603815, 85.5441724 , 87.79230665, 90.0404409 , 92.28857515, 94.53670941, 96.78484366, 99.03297791, 101.28111216, 103.52924641, 105.77738067, 108.02551492, 110.27364917, 112.52178342, 114.76991767, 117.01805193, 119.26618618, 121.51432043, 123.76245468, 126.01058893, 128.25872319, 130.50685744, 132.75499169, 135.00312594, 137.25126019, 139.49939445, 141.7475287 , 143.99566295, 146.2437972 , 148.49193145, 150.74006571, 152.98819996]), <a list of 50 Patch objects>)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/3b8c309fb83f4522acfff6f2728f88e4~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=s%2Bi7Aq%2BIMtuV8RHIbql4aEkfhho%3D)


【2】概率密度



mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000)

plt.hist(x, 50, density=True, color="r")# 概率密度图 plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'μ=100, σ=15\mu=100,\ \sigma=15') plt.xlim(40, 160) plt.ylim(0, 0.03)



(0, 0.03)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/0dbbfa00f1b2436f9c2a6b35ea5b3c81~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=4IhNj6eiiaVLiyK6b4jXo8xAS0E%3D)



mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000)

plt.hist(x, bins=50, density=True, color="r", histtype='step') #不填充,只获得边缘 plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'μ=100, σ=15\mu=100,\ \sigma=15') plt.xlim(40, 160) plt.ylim(0, 0.03)



(0, 0.03)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/c2e939ee9861479f8c62d56bc1b04f95~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=mb1St3WPXgmDdlSiHnXy9wH%2Bh%2FY%3D)



from scipy.stats import norm mu, sigma = 100, 15 # 想获得真正高斯分布的概率密度图 x = mu + sigma * np.random.randn(10000)

先获得bins,即分配的区间

_, bins, __ = plt.hist(x, 50, density=True) y = norm.pdf(bins, mu, sigma) # 通过norm模块计算符合的概率密度 plt.plot(bins, y, 'r--', lw=3)
plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'μ=100, σ=15\mu=100,\ \sigma=15') plt.xlim(40, 160) plt.ylim(0, 0.03)



(0, 0.03)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/31bf9397b12e4f9497c3453a3319ad81~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=K8o8JBzW41BgmUQRHzRzr%2BEIOkY%3D)


【3】累计概率分布



mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000)

plt.hist(x, 50, density=True, cumulative=True, color="r") # 将累计cumulative设置为true即可 plt.xlabel('Smarts') plt.ylabel('Cum_Probability') plt.title('Histogram of IQ') plt.text(60, 0.8, r'μ=100, σ=15\mu=100,\ \sigma=15') plt.xlim(50, 165) plt.ylim(0, 1.1)



(0, 1.1)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/7b57314b29e44662b4832a3022f3f271~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=9zZKTlNUMvYc2BR69cv%2BnipGiJw%3D)


【例】模拟投两个骰子



class Die(): "模拟一个骰子的类"

def \_\_init\_\_(self, num_sides=6):
    self.num_sides = num_sides

def roll(self):
    return np.random.randint(1, self.num_sides+1)

* 重复投一个骰子



die = Die() results = [] for i in range(60000): result = die.roll() results.append(result)

plt.hist(results, bins=6, range=(0.75, 6.75), align="mid", width=0.5) plt.xlim(0 ,7)



(0, 7)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/292b017a66e941f6965e16ca34e56ca8~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=zuZjTI5qSl%2BeD75zuQuZHS2gVu8%3D)


* 重复投两个骰子



die1 = Die() die2 = Die() results = [] for i in range(60000): result = die1.roll()+die2.roll() results.append(result)

plt.hist(results, bins=11, range=(1.75, 12.75), align="mid", width=0.5) plt.xlim(1 ,13) plt.xticks(np.arange(1, 14))



([<matplotlib.axis.XTick at 0x2052fae23c8>, <matplotlib.axis.XTick at 0x2052ff1fa20>, <matplotlib.axis.XTick at 0x2052fb493c8>, <matplotlib.axis.XTick at 0x2052e9b5a20>, <matplotlib.axis.XTick at 0x2052e9b5e80>, <matplotlib.axis.XTick at 0x2052e9b5978>, <matplotlib.axis.XTick at 0x2052e9cc668>, <matplotlib.axis.XTick at 0x2052e9ccba8>, <matplotlib.axis.XTick at 0x2052e9ccdd8>, <matplotlib.axis.XTick at 0x2052fac5668>, <matplotlib.axis.XTick at 0x2052fac5ba8>, <matplotlib.axis.XTick at 0x2052fac5dd8>, <matplotlib.axis.XTick at 0x2052fad9668>], <a list of 13 Text xticklabel objects>)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/cd995980a31d471b95eb7a66c66813bb~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=KKyy%2FLb3Z78bOg3y356FexwvbPk%3D)


#### 13.1.6 误差图


【1】基本误差图



x = np.linspace(0, 10 ,50) dy = 0.5 # 每个点的y值误差设置为0.5 y = np.sin(x) + dy*np.random.randn(50)

plt.errorbar(x, y , yerr=dy, fmt="+b")



<ErrorbarContainer object of 3 artists>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/6a8fd1e33d224533893e7d77ba394d74~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=hw5eo9ihnARRdi%2FrHJtzB9dSt5I%3D)


【2】柱形图误差图



menMeans = (20, 35, 30, 35, 27) womenMeans = (25, 32, 34, 20, 25) menStd = (2, 3, 4, 1, 2) womenStd = (3, 5, 2, 3, 3) ind = ['G1', 'G2', 'G3', 'G4', 'G5'] width = 0.35

p1 = plt.bar(ind, menMeans, width=width, label="Men", yerr=menStd) p2 = plt.bar(ind, womenMeans, width=width, bottom=menMeans, label="Men", yerr=womenStd)

plt.ylabel('Scores') plt.title('Scores by group and gender') plt.yticks(np.arange(0, 81, 10)) plt.legend()



<matplotlib.legend.Legend at 0x20531035630>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/80605f4c627448c39cddd269d17342c2~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=wsvNjxQgStwZNanrdopZtNUoexs%3D)


#### 13.1.7 面向对象的风格简介


【例1】 普通图



x = np.linspace(0, 5, 10) y = x ** 2

fig = plt.figure(figsize=(8,4), dpi=80) # 图像 axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # 轴 left, bottom, width, height (range 0 to 1)

axes.plot(x, y, 'r') axes.set_xlabel('x') axes.set_ylabel('y') axes.set_title('title')



Text(0.5, 1.0, 'title')


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/757380f668104cf09989c5b83483b84a~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=QYK%2BHEJoXl0UUrNrP9iKOwC34O8%3D)


【2】画中画



x = np.linspace(0, 5, 10) y = x ** 2

fig = plt.figure()

ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) ax2 = fig.add_axes([0.2, 0.5, 0.4, 0.3])

ax1.plot(x, y, 'r')

ax1.set_xlabel('x') ax1.set_ylabel('y') ax1.set_title('title')

ax2.plot(y, x, 'g') ax2.set_xlabel('y') ax2.set_ylabel('x') ax2.set_title('insert title')



Text(0.5, 1.0, 'insert title')


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/6ab75f2eeb5c4c1ebe8830cac7071bc2~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=LSBngPv8YteHM3kn3O2mYSk4XnY%3D)


【3】 多子图



def f(t): return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 3.0, 0.01)

fig= plt.figure() fig.subplots_adjust(hspace=0.4, wspace=0.4)

ax1 = plt.subplot(2, 2, 1) ax1.plot(t1, f(t1)) ax1.set_title("Upper left")

ax2 = plt.subplot(2, 2, 2) ax2.plot(t1, f(t1)) ax2.set_title("Upper right")

ax3 = plt.subplot(2, 1, 2) ax3.plot(t1, f(t1)) ax3.set_title("Lower")



Text(0.5, 1.0, 'Lower')


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/9ca60260940043afb1f00389fbb4f0a7~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=M69Mi8pYgHD3Iky32ghB%2BAY8NJo%3D)


#### 13.1.8 三维图形简介


【1】三维数据点与线



from mpl_toolkits import mplot3d # 注意要导入mplot3d

ax = plt.axes(projection="3d") zline = np.linspace(0, 15, 1000) xline = np.sin(zline) yline = np.cos(zline) ax.plot3D(xline, yline ,zline)# 线的绘制

zdata = 15*np.random.random(100) xdata = np.sin(zdata) ydata = np.cos(zdata) ax.scatter3D(xdata, ydata ,zdata, c=zdata, cmap="spring") # 点的绘制



<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x2052fd1e5f8>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/b1a09dda3d4844239b1c20da0abb6dab~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=J7iYh1hZ0uRnceaKcvH5c6f5E4A%3D)


【2】三维数据曲面图



def f(x, y): return np.sin(np.sqrt(x**2 + y**2))

x = np.linspace(-6, 6, 30) y = np.linspace(-6, 6, 30) X, Y = np.meshgrid(x, y) # 网格化 Z = f(X, Y)

ax = plt.axes(projection="3d") ax.plot_surface(X, Y, Z, cmap="viridis") # 设置颜色映射



<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x20531baa5c0>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/6dc66f59002d4d538ee5cf435dce83dc~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=3WlfUUagtSZzVqEBqO0Q7t%2B4HE4%3D)



import numpy as np import matplotlib.pyplot as plt from mpl_toolkits import mplot3d

t = np.linspace(0, 2*np.pi, 1000) X = np.sin(t) Y = np.cos(t) Z = np.arange(t.size)[:, np.newaxis]

ax = plt.axes(projection="3d") ax.plot_surface(X, Y, Z, cmap="viridis")



<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x1c540cf1cc0>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/a6d66b7853aa45e0b7821abefe24b951~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=FvFBEQiW%2B%2B9gWxUzQ5kBIIy5%2BGc%3D)


### 13.2 Seaborn库-文艺青年的最爱


【1】Seaborn 与 Matplotlib


Seaborn 是一个基于 matplotlib 且数据结构与 pandas 统一的统计图制作库



x = np.linspace(0, 10, 500) y = np.cumsum(np.random.randn(500, 6), axis=0)

with plt.style.context("classic"): plt.plot(x, y) plt.legend("ABCDEF", ncol=2, loc="upper left")


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/f8d5b65d0fe74b6e982433e0aea3ee62~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=vWUaVU3jmv6YSHx6tbHPI5NJBkk%3D)



import seaborn as sns

x = np.linspace(0, 10, 500) y = np.cumsum(np.random.randn(500, 6), axis=0) sns.set()# 改变了格式 plt.figure(figsize=(10, 6)) plt.plot(x, y) plt.legend("ABCDEF", ncol=2, loc="upper left")



<matplotlib.legend.Legend at 0x20533d825f8>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/28978e10cf3640c6926309e8155303d3~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=EbpLeZuUOBkF7jpWQzi7d3ZCoyo%3D)


【2】柱形图的对比



x = ['G1', 'G2', 'G3', 'G4', 'G5'] y = 2 * np.arange(1, 6)

plt.figure(figsize=(8, 4)) plt.barh(x, y, align="center", height=0.5, alpha=0.8, color="blue") plt.tick_params(axis="both", labelsize=13)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/e95eafc1115f41cb89defa69a89cae79~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=2V7%2FgSicmiAyg%2BKHLdLff6VY5Z4%3D)



import seaborn as sns

plt.figure(figsize=(8, 4)) x = ['G5', 'G4', 'G3', 'G2', 'G1'] y = 2 * np.arange(5, 0, -1) #sns.barplot(y, x) sns.barplot(y, x, linewidth=5)



<matplotlib.axes._subplots.AxesSubplot at 0x20533e92048>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/c56cca58ff5045439af8a43d5aad6c6a~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=CRDpW%2FyvmutoHfvP%2FYA5co5fjr4%3D)



sns.barplot?


【3】以鸢尾花数据集为例



iris = sns.load_dataset("iris")



iris.head()






|  | sepal\_length | sepal\_width | petal\_length | petal\_width | species |
| --- | --- | --- | --- | --- | --- |
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |



sns.pairplot(data=iris, hue="species")



<seaborn.axisgrid.PairGrid at 0x205340655f8>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/0e9f5978d1a043e7ac76931c660d483b~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=6X%2BELjX3WjrNrEv8PRYJMAZ5UOE%3D)


### 13.3 Pandas 中的绘图函数概览



import pandas as pd


【1】线形图



df = pd.DataFrame(np.random.randn(1000, 4).cumsum(axis=0), columns=list("ABCD"), index=np.arange(1000)) df.head()






|  | A | B | C | D |
| --- | --- | --- | --- | --- |
| 0 | -1.311443 | 0.970917 | -1.635011 | -0.204779 |
| 1 | -1.618502 | 0.810056 | -1.119246 | 1.239689 |
| 2 | -3.558787 | 1.431716 | -0.816201 | 1.155611 |
| 3 | -5.377557 | -0.312744 | 0.650922 | 0.352176 |
| 4 | -3.917045 | 1.181097 | 1.572406 | 0.965921 |



df.plot()



<matplotlib.axes._subplots.AxesSubplot at 0x20534763f28>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/9646c0f241994c01a6834986d39d6d4b~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=8V1B7NJe2dgMr7t71z6ZUhOrIhE%3D)



df = pd.DataFrame() df.plot?


【2】柱形图



df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd']) df2






|  | a | b | c | d |
| --- | --- | --- | --- | --- |
| 0 | 0.587600 | 0.098736 | 0.444757 | 0.877475 |
| 1 | 0.580062 | 0.451519 | 0.212318 | 0.429673 |
| 2 | 0.415307 | 0.784083 | 0.891205 | 0.756287 |
| 3 | 0.190053 | 0.350987 | 0.662549 | 0.729193 |
| 4 | 0.485602 | 0.109974 | 0.891554 | 0.473492 |
| 5 | 0.331884 | 0.128957 | 0.204303 | 0.363420 |
| 6 | 0.962750 | 0.431226 | 0.917682 | 0.972713 |
| 7 | 0.483410 | 0.486592 | 0.439235 | 0.875210 |
| 8 | 0.054337 | 0.985812 | 0.469016 | 0.894712 |
| 9 | 0.730905 | 0.237166 | 0.043195 | 0.600445 |


* 多组数据竖图



df2.plot.bar()



<matplotlib.axes._subplots.AxesSubplot at 0x20534f1cb00>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/885ad876d24440679b4f797192c7d650~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=gquUQiSpbz%2FZHawTDOgaTF7Jbig%3D)


* 多组数据累加竖图



df2.plot.bar(stacked=True) # 累加的柱形图



<matplotlib.axes._subplots.AxesSubplot at 0x20534f22208>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/2d631adf51384cdb92bcf8c398573503~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=xe9jn9eftDmOnWbyrYoP1BrbCZk%3D)


* 多组数据累加横图



df2.plot.barh(stacked=True) # 变为barh



<matplotlib.axes._subplots.AxesSubplot at 0x2053509d048>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/c28c052bd34b4254a06722060513504d~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=ANnEJ%2FUlJG7jMdiaiZ4aktpZ2Uw%3D)


【3】直方图和密度图



df4 = pd.DataFrame({"A": np.random.randn(1000) - 3, "B": np.random.randn(1000), "C": np.random.randn(1000) + 3}) df4.head()






|  | A | B | C |
| --- | --- | --- | --- |
| 0 | -4.250424 | 1.043268 | 1.356106 |
| 1 | -2.393362 | -0.891620 | 3.787906 |
| 2 | -4.411225 | 0.436381 | 1.242749 |
| 3 | -3.465659 | -0.845966 | 1.540347 |
| 4 | -3.606850 | 1.643404 | 3.689431 |


* 普通直方图



df4.plot.hist(bins=50)



<matplotlib.axes._subplots.AxesSubplot at 0x20538383b38>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/e0da7302941a4306b3a3440e6814de04~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=vy7BKaKsiXRTjQ6rIc8WGAI7YgI%3D)


* 累加直方图



df4['A'].plot.hist(cumulative=True)



<matplotlib.axes._subplots.AxesSubplot at 0x2053533bbe0>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/024eb2b1551a44eb98e77ed5d93901d8~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=JpeGifI7MACFYcZpfwB9Y0o58Ko%3D)


* 概率密度图



df4['A'].plot(kind="kde")



<matplotlib.axes._subplots.AxesSubplot at 0x205352c4e48>


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/438829b527c846e38507be57751845f3~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=mUezTINRb4dyA7FsdxZgXy%2BriDU%3D)


* 差分



df = pd.DataFrame(np.random.randn(1000, 4).cumsum(axis=0), columns=list("ABCD"), index=np.arange(1000)) df.head()






|  | A | B | C | D |
| --- | --- | --- | --- | --- |
| 0 | -0.277843 | -0.310656 | -0.782999 | -0.049032 |
| 1 | 0.644248 | -0.505115 | -0.363842 | 0.399116 |
| 2 | -0.614141 | -1.227740 | -0.787415 | -0.117485 |
| 3 | -0.055964 | -2.376631 | -0.814320 | -0.716179 |
| 4 | 0.058613 | -2.355537 | -2.174291 | 0.351918 |



df.diff().hist(bins=50, color="r")



array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000002053942A6A0>, <matplotlib.axes._subplots.AxesSubplot object at 0x000002053957FE48>], [<matplotlib.axes._subplots.AxesSubplot object at 0x00000205395A4780>, <matplotlib.axes._subplots.AxesSubplot object at 0x00000205395D4128>]], dtype=object)


![png](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/f9ada86aeee64c1ba1571b0d60d1d93b~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771413811&x-signature=aAQ92QxNOQzf0ZDNyrKxTmURYx8%3D)



df = pd.DataFrame() df.hist?


【4】散点图



housing = pd.read_csv("housing.csv") housing.head()






|  | longitude | latitude | housing\_median\_age | total\_rooms | total\_bedrooms | population | households | median\_income | median\_house\_value | ocean\_proximity |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 0 | -122.23 | 37.88 | 41.0 | 880.0 | 129.0 | 322.0 | 126.0 | 8.3252 | 452600.0 | NEAR BAY |
| 1 | -122.22 | 37.86 | 21.0 | 7099.0 | 1106.0 | 2401.0 | 1138.0 | 8.3014 | 358500.0 | NEAR BAY |
| 2 | -122.24 | 37.85 | 52.0 | 1467.0 | 190.0 | 496.0 | 177.0 | 7.2574 | 352100.0 | NEAR BAY |
| 3 | -122.25 | 37.85 | 52.0 | 1274.0 | 235.0 | 558.0 | 219.0 | 5.6431 | 341300.0 | NEAR BAY |
| 4 | -122.25 | 37.85 | 52.0 | 1627.0 | 280.0 | 565.0 | 259.0 | 3.8462 | 342200.0 | NEAR BAY |



"""基于地理数据的人口、房价可视化"""

圆的半价大小代表每个区域人口数量(s),颜色代表价格(c),用预定义的jet表进行可视化

with sns.axes_style("white"): housing.plot(kind="scatter", x="longitude", y="latitude", alpha=0.6, s=housing["population"]/100, label="population", c="median_house_value", cmap="jet", colorbar=True, figsize=(12, 8)) plt.legend() plt.axis([-125, -113.5, 32, 43])



[-125, -113.5, 32, 43]

(1)Python所有方向的学习路线(新版)

这是我花了几天的时间去把Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

最近我才对这些路线做了一下新的更新,知识体系更全面了。

在这里插入图片描述

(2)Python学习视频

包含了Python入门、爬虫、数据分析和web开发的学习视频,总共100多个,虽然没有那么全面,但是对于入门来说是没问题的,学完这些之后,你可以按照我上面的学习路线去网上找其他的知识资源进行进阶。

在这里插入图片描述

(3)100多个练手项目

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了,只是里面的项目比较多,水平也是参差不齐,大家可以挑自己能做的项目去练练。

在这里插入图片描述

了解详情:docs.qq.com/doc/DSnl3ZG…