Python极简读书笔记(八)matpltlib与Seaborn可视化分析

273 阅读3分钟

image-20221121164219277image-20221121164219277

image-20221121164329919

seaborn是比matplotlib更高级的绘图库

import math
import matplotlib.pyplot as plt
​
# 生成正弦曲线x点
nbSamples = 256
xRange = (-math.pi, math.pi) 
x, y = [], []
​
# 构造数据很麻烦
for n in range(nbSamples):
    ratio = (n + 0.5) / nbSamples
    x.append(xRange[0] + (xRange[1] - xRange[0]) * ratio)
    y.append(math.sin(x[-1]))
​
plt.plot(x, y)
plt.show()
# 使用NumPy简化数据构造
import math
import matplotlib.pyplot as plt
import numpy as np
​
# 生成正弦曲线
nbSamples = 256
x = np.linspace(-math.pi, math.pi, num=256)
y = np.sin(x)
​
plt.plot(x, y)
plt.show()

image-20221121172340814

  • color
  • linewidth
  • linestyle

pyplot高级功能

  • 图例和注释

    • # 使用NumPy简化数据构造
      import math
      import matplotlib.pyplot as plt
      import numpy as np
      ​
      # 生成正弦曲线
      nbSamples = 256
      x = np.linspace(-math.pi, math.pi, num=256)
      y = np.sin(x)
      y2 = np.cos(x)
      ​
      plt.plot(x, y, label=r'$y=sin(x)$')
      plt.plot(x, y2)
      ​
      plt.legend(loc='best')
      plt.show()
      
  • 显示坐标点

    • # 使用NumPy简化数据构造
      import math
      import matplotlib.pyplot as plt
      import numpy as np
      ​
      # 生成正弦曲线
      nbSamples = 256
      x = np.linspace(-math.pi, math.pi, num=256)
      y = np.sin(x)
      y2 = np.cos(x)
      ​
      plt.plot(x, y, label=r'$y=sin(x)$')
      for a,b in zip(x, y):
          plt.text(a, b, (a, b))
      plt.plot(x, y2)
      ​
      plt.legend(loc='best')
      plt.show()
      
  • 标题&坐标轴

    • plt.rcParams['font.sans-serif']=['SimHei']
      plt.title('正弦函数')
      ​
      plt.xlim(-5, 5)
      plt.xlabel('iamx')
      

      image-20221121173701209

  • 一次性多条曲线

    • image-20221121173757911
  • 添加网格线

    • plt.grid(b = True)
  • 绘制多个子图

t2 = np.arange(0.0, 5.0, 0.1)
​
plt.subplot(211)
plt.plot(t2, t2)
​
plt.subplot(2,1,2)
plt.plot(t2, np.cos(2*np.pi*t2))
plt.show()

image-20221121192714177

  • Axes和Subplot的区别

    • image-20221121192912487

    • 整齐排列的是子图;不规则摆放的叫Axes

      • import matplotlib.pyplot as plt
        ​
        fig = plt.figure()
        fig, axes_lst = plt.subplots(2, 2)
        plt.show(), axes_lst
        ​
        >>>>>>>
        (None,
         array([[<AxesSubplot:>, <AxesSubplot:>],
                [<AxesSubplot:>, <AxesSubplot:>]], dtype=object))
        

        image-20221121194137804

      • import matplotlib.pyplot as plt
        ​
        fig = plt.figure()
        fig, axes_lst = plt.subplots(2, 2)
        ​
        t2 = np.arange(0.0, 5.0, 0.1)
        # 就写到第四个位置
        axes_lst[1,1].plot(t2, np.cos(2*np.pi*t2))
        ​
        plt.show(), axes_lst
        

        image-20221121194436369

  • 实现大图套小图效果

    • 很有意思

    • import matplotlib.pyplot as plt
      
      x = np.linspace(0, 2*np.pi, 400)
      y = np.sin(x**2)
      
      fig = plt.figure()
      l1, b1, w1, h1 = 0.1, 0.1, 0.8, 0.8
      # 画布上增加子图
      a1 = fig.add_axes([l1, b1, w1, h1])
      a1.scatter(x, y)
      a1.set_xlabel('x')
      a1.set_ylabel('y')
      
      a1.set_title('title')
      
      l2, b2, w2, h2 = 0.6, 0.6, 0.25, 0.25
      # 画布上增加另一个子图
      a2 = fig.add_axes([l2, b2, w2, h2])
      a2.plot(x, y)
      a2.set_title('title inside')
      plt.show()
      

      image-20221121195246165

  • 散点图

    • plt.scatter()
    • 只能绘制点状图,不支持将点连成线
  • 条形图 plt.bar()

    • 一般条形图:bar() barh()

      • bar() 垂直条形图
      • barh() 水平条形图
    • 并列条形图:

      • import numpy as np
        import matplotlib.pyplot as plt
        
        n_groups = 4
        means_f = (90,55,40,65)
        means_g = (85,62,54,20)
        
        fig, ax = plt.subplots()
        index = np.arange(n_groups)
        
        bar_width = 0.35
        r1 = plt.bar(index, means_f, bar_width, label='r1')
        r1 = plt.bar(index+bar_width, means_g, bar_width, label='r2')
        plt.xlabel('lesson')
        plt.ylabel('score')
        plt.legend()
        plt.show()
        

        image-20221121200826097

    • 叠加条形图

      • import numpy as np
        import matplotlib.pyplot as plt
        
        n_groups = 4
        means_f = (90,55,40,65)
        means_g = (85,62,54,20)
        
        fig, ax = plt.subplots()
        index = np.arange(n_groups)
        
        bar_width = 0.35
        r1 = plt.bar(index, means_f, bar_width, label='r1')
        r1 = plt.bar(index, means_g, bar_width, bottom=means_f, label='r2')
        plt.xlabel('lesson')
        plt.ylabel('score')
        plt.legend()
        plt.show()
        

        image-20221121200955755

  • 直方图 plt.hist()

    • 直方图的宽度也是有意义的,条形图的宽度是没有意义的

    • X轴会从小到大划分为若干个间隔,间隔越大涵盖的属性跨度越大。

    • Y轴表示特定区间样本出现的次数(频数)

    • import numpy as np
      import matplotlib.pyplot as plt
      
      mu = 100
      sigma = 15
      x = mu + sigma * np.random.randn(200)
      num_bins = 25 # x轴间隔
      plt.figure(figsize=(9,6), dpi=100)
      
      n,bins,patches = plt.hist(x, num_bins)
      
      plt.show(), bins, patches, n
      

      image-20221121202745606

  • 饼图 plt.pie

    • image-20221123162233579
    • image-20221123162255364
  • 箱形图 boxplot plt.boxplot()

image-20221123162422994

  • 误差条 Error Bar plt.errorbar()

    • image-20221123162534849

绘制三维图形

from mpl_toolkits.mplot3d import Axes3D

image-20221123162639672

与Pandas协作绘图

matplotlib是一个相当底层的绘图工具,Matplotlib被有机集成到了Pandas中。

df = pd.read_csv('us.csv', parse_dates = True, index_col = [0])
# 将日期类型称为DataFrame对象的索引
df.plot() 

image-20221123163533068

# 绘制某个州
df.Illinois.plot()
plt.show()

image-20221123163615602

  • pd绘制子图

    • df[a].plot(subplots = True, figsize = (15,60), layout=(3,4))
      

      image-20221123163833819

惊艳的Seaborn

基于matlab的数据可视化库,进行了更高级的API封装,使图像变得精致

image-20221125153622208

对图 pairplot

  • sns.pairplot()

image-20221125154103209

sns.set_style("ticks") #用于设置图片风格,darkgrid/whitegrid/dark/white/ticks

heatmap(热力图)

  • sns.heatmap()

    image-20221125154505539

箱形图 bo x p lo t

  • sns.boxplot()

image-20221125154629522

image-20221125154639130

小提琴图 violinplot

  • sns.violinplot()

image-20221125154723807

密度图 Density Plot

  • sns.kdeplot()

image-20221125154816158

seaborn这个简单看看就行了,要用的时候和echart一样官网看看api。