目录

  • matplotlib库学习
  • 1.官网
  • 2.概述
  • 3.体验
  • 4.修改线的颜色,加标签等等操作
  • 5.Matplotlib使用流程
  • 6.Matplotlib编程模型
  • 1.绘制图表的老方法
  • 2.绘制图表的新方法
  • pandas库学习
  • 基本代码

matplotlib库学习

1.官网

https://matplotlib.org/

2.概述

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.
【Matplotlib就是一个数据可视化的东西】
和大数据可视化有区别
【Matplotlib他是一个数据挖掘的辅助工具】
数据可视化框架
1.echarts
2.superset
3.kidannan

3.体验

import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':

    x = np.linspace(0, 2 * np.pi, 200)
    y = np.sin(x)

    fig, ax = plt.subplots()
    ax.plot(x, y)
    plt.show()

4.修改线的颜色,加标签等等操作

import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
    # 1.数据准备
    x = np.arange(0, 1, 0.05)
    # y = sin(2*pi*x)
    y = np.sin(2*np.pi*x)


    # 2.快速画图
    plt.plot(x,y)
    # 修改线条颜色
    plt.plot(x,y,'r')
    # 修改线条颜色和样式
    plt.plot(x, y, 'r--')
    # 修改线条颜色和样式 显示数据点
    plt.plot(x, y, 'r--*')

    # 3.添加 图表 标题、x轴、y轴
    plt.plot(x, y, 'r--*')
    plt.title("y = sin(2*pi*x)")
    plt.xlabel("x")
    plt.ylabel("y")

    # 4.添加图例 + 指定标签位置
    plt.plot(x, y, 'r--*',label='sin')
    plt.title("y = sin(2*pi*x)")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend(loc="best")
    
    #  Troubleshooting 解决问题的能力

    # last显示图片
    plt.show()

5.Matplotlib使用流程

1.绘图
2.图标:属性
线条:颜色、样式、数据点
图标额外:轴标签(x,y)、图例、标题
3.有哪些图标
折线图、柱状图、散点图、饼状图

6.Matplotlib编程模型

1.Figures 画布
2.Axes 图表
绘制图表步骤
1.准备画布
2.添加图表 1 2
ax1 = fig.add_subplot(222) 3 4
画布分成两行两列 从左到右 从上到下 第2块
3.添加数据

1.绘制图表的老方法

import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
    # 1.数据准备
    x = np.arange(0, 1, 0.05)
    # y = sin(2*pi*x)
    y = np.sin(2*np.pi*x)
    '''
    # 2.画布 + 多个图表 1 2
    #                 3 4
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax2 = fig.add_subplot(222)
    ax3 = fig.add_subplot(223)
    ax4 = fig.add_subplot(224)


    # 3.画布 + 多个图表 1 2 + 数据
    #                 3 4
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax2 = fig.add_subplot(222)
    ax3 = fig.add_subplot(223)
    ax4 = fig.add_subplot(224)

    ax2.plot(x,y)
    plt.show()

    # 4.画布 + 多个图表 1 2 + 数据
    #                 3 4
    # 修改曲线的颜色 线条样式 显示数据点【简版】
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax2 = fig.add_subplot(222)
    ax3 = fig.add_subplot(223)
    ax4 = fig.add_subplot(224)

    ax2.plot(x, y,'r--o')
    plt.show()
    '''
    # 4.画布 + 多个图表 1 2 + 数据
    #                 3 4
    # 修改曲线的颜色 线条样式 显示数据点【常规版】
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax2 = fig.add_subplot(222)
    ax3 = fig.add_subplot(223)
    ax4 = fig.add_subplot(224)

    ax2.plot(x, y,color='c',linestyle='--',marker='o')
    plt.show()

2.绘制图表的新方法

import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
    # 1.数据准备
    x = np.arange(0, 1, 0.05)
    # y = sin(2*pi*x)
    y = np.sin(2*np.pi*x)
    y2 = np.cos(2*np.pi*x)

    # new - 使用一个曲线
    fig, ax = plt.subplots()  # Create a figure containing a single axes.
    ax.plot(x,y,'g--*',label='sin') # 颜色、样式、标记、图例
    ax.set(title='sin 01',xlabel='x',ylabel='y') # 标题 x y
    ax.legend(loc='best') # 参数位置
    ax.grid() # 添加网格线
    plt.show()

    # new - 使用多个曲线
    fig, ax = plt.subplots()  # Create a figure containing a single axes.
    ax.plot(x, y, 'g--*', label='sin')  # 颜色、样式、标记、图例
    ax.plot(x, y2, 'r--*', label='cos')  # 添加曲线
    # 通用属性
    ax.set(title='sin&cos', xlabel='x', ylabel='y')  # 标题 x y
    ax.legend(loc='best')  # 参数位置
    ax.grid()  # 添加网格线
    plt.show()

    # 保存 图表
    fig.savefig('1.jpg')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
if __name__ == '__main__':
    # 1.数据准备
    df = pd.read_csv(r'D:\ \python-sk\0408study\data.csv',index_col="年份")
    # 2.绘制图表 matplotlib
    #     准备数据 x y
    x = df.index.values
    # print(x)
    y = df['啤酒产量(万千升)'].values
    # print(y)
    from pylab import mpl
    mpl.rcParams['font.sans-serif'] = ['FangSong']


    # 画折线图
    flg, ax = plt.subplots()
    ax.plot(x,y,'r--*')
    ax.set(title='啤酒产量走势', xlabel='年份', ylabel='啤酒产量')  # 标题 x y
    plt.show()

    # 画柱状图
    flg, ax = plt.subplots()
    ax.bar(x, y, width=0.8, color ="skyblue", linewidth=0.5)
    ax.set(title='啤酒产量走势', xlabel='年份', ylabel='啤酒产量')  # 标题 x y
    plt.show()

    # 画水平柱状图
    flg, ax = plt.subplots()
    ax.barh(x, y, color="skyblue", linewidth=0.5)
    ax.set(title='啤酒产量走势', xlabel='年份', ylabel='啤酒产量')  # 标题 x y
    plt.show()

    # 画饼图
    fig,ax = plt.subplots()
    ax.pie(y,labels=x)
    ax.set(title='啤酒产量走势', xlabel='年份', ylabel='啤酒产量')  # 标题 x y
    plt.show()
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
if __name__ == '__main__':
    # 1.数据准备
    df = pd.read_csv(r'D:\ \python-sk\0408study\BankData.csv',index_col="分行编号")
    # 2.绘制图表 matplotlib
    #     准备数据 x y

    x = df['各项贷款余额']
    y = df['不良贷款(亿元)']

    # 指定默认字体(防止中文出现乱码)
    from pylab import mpl
    mpl.rcParams['font.sans-serif'] = ['FangSong']  # 指定‘仿宋’字体

    # 5.散点图
    fig, ax = plt.subplots()
    ax.scatter(x, y, alpha=0.5)  # alpha是设置透明度,这样点重合时看的明显
    ax.set(title='散点图')
    ax.set(xlabel='各项贷款余额')
    ax.set(ylabel='不良贷款(亿元)')
    # 显示图形
    plt.show()

    # 6.散点图 多个图表
    fig, ax = plt.subplots(2, 2)
        #   0   1
        #0 0,0 0,1
        #1 1,0 1,1
    ax[0,1].scatter(x, y, alpha=0.5)  # alpha是设置透明度,这样点重合时看的明显
    # 显示图形
    plt.show()

pandas库学习

基本代码

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
if __name__ == '__main__':
    # 1.数据准备
    df = pd.read_csv(r'D:\ \python-sk\0408study\data.csv',index_col="年份")
    print(df.head())

    # 指定默认字体(防止中文出现乱码)
    from pylab import mpl
    mpl.rcParams['font.sans-serif'] = ['FangSong']  # 指定‘仿宋’字体

    # 2.绘制图表 pandas
    df['人均GDP(元)'].plot(color='r')
    plt.show()
    df['人均GDP(元)'].plot(kind='bar',color='r')
    plt.show()
    df['人均GDP(元)'].plot(kind='barh', color='r')
    plt.show()
    df['人均GDP(元)'].plot(kind='hist', color='r')
    plt.show()
    df['人均GDP(元)'].plot(kind='area', color='r')
    plt.show()
    df['人均GDP(元)'].plot(kind='pie')
    plt.show()
    df.plot(kind='scatter',x="人均GDP(元)", y="啤酒产量(万千升)")
    plt.show()

    df.plot(kind='scatter', x="人均GDP(元)", y="啤酒产量(万千升)", c="居民消费价格指数(上面=100)", cmap="viridis")
    plt.show()