可能是最精辟的Python matplot笔记

matplot 绘图

数据准备

import pandas as pd
idCardInfo = pd.read_csv("idcard.csv")
idCardInfo.head()

可能是最精辟的Python matplot笔记

空图绘制

import matplotlib.pyplot as plt
plt.plot()
plt.show()

可能是最精辟的Python matplot笔记

绘二维图

基础绘制

plt.plot(idCardInfo["confidence"],idCardInfo["money"])
plt.show()

可能是最精辟的Python matplot笔记

完善图例

plt.plot(idCardInfo["confidence"],idCardInfo["money"])
plt.yticks(rotation=45) # y轴刻度逆时针旋转45°
plt.xticks(rotation=45) # x轴刻度逆时针旋转45°
plt.xlabel("confidence") # x轴图例
plt.ylabel("money") # y轴图例
plt.title("confidence - money") # 标题
plt.show()

可能是最精辟的Python matplot笔记

绘制多图

多个空图

fig = plt.figure()
sf1 = fig.add_subplot(2,2,1) # 2x2 第一个位置
sf2 = fig.add_subplot(2,2,2) # 2x2 第四个位置
sf4 = fig.add_subplot(2,2,4) # 2x2 第四个位置
plt.show()

可能是最精辟的Python matplot笔记

多个二维图

fig = plt.figure()
sf1 = fig.add_subplot(2,2,1)
sf2 = fig.add_subplot(2,2,4)
sf1.plot(idCardInfo["money"],idCardInfo["confidence"],c='red')
sf2.plot(idCardInfo["confidence"],idCardInfo["money"])
plt.show()

可能是最精辟的Python matplot笔记

多数据图

import numpy as np
fig = plt.figure(figsize=(16,6)) # 16:6 宽度
colors = ['red', 'blue', 'green', 'orange', 'black']
h = np.arange(10,100,10) # 10 - 100(不包) 间隔 10
for i in range(5):
    v = 1000*i + np.random.randn(9,1) * 1000 
    # label = str(i*10)
    plt.plot(h, v, c=colors[i],label=label) # lable 图例显示内容(中文需处理)
plt.legend(loc='best')
plt.show()

可能是最精辟的Python matplot笔记

特殊图像

data = np.random.randn(2, 100)

fig, axs = plt.subplots(2, 2, figsize=(10, 10))
axs[0, 0].hist(data[0])
axs[1, 0].scatter(data[0], data[1])
axs[0, 1].plot(data[0], data[1])
axs[1, 1].hist2d(data[0], data[1])

plt.show()

可能是最精辟的Python matplot笔记