商務統計_7 用圖表演示數據 - 定量數據

目錄

  • 定量數據
    • 時間序列圖(推移圖)
      時間(或發生次序)爲橫軸,變量爲縱軸。反映變量隨時間變化的趨勢。
    • 面積圖(區域圖)
      強調數量的變化程度,反映總值趨勢。
    • 散點圖
      橫縱座標分別代表兩個變量的取值。用於描述兩個變量之間的關係。
    • 對數圖
      時間爲橫軸,變量的10爲底的對數爲縱軸。通常用於反映增長率的變化趨勢。
  • 附1. 增加縱軸單位標識
  • 附2. matplotlib.gridspec參數
  • 附3. matplotlib.ticker.FormatStrFormatter參數


定量數據

import matplotlib.pyplot as plt
import pandas as pd

# 顯示中文       
plt.rcParams['font.family'] = ['Microsoft YaHei']

1.時間序列圖(time series plot)
又稱推移圖,以時間(或發生次序)爲橫軸,變量爲縱軸的折線圖。
常用於質量管理,觀察變量是否隨時間變化而呈某種趨勢。
優缺:


  • 直觀反映數據變化的趨勢;比較多個時間序列數據各個時期的變化情況;
dic = {'年份': [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
              2009, 2010, 2011, 2012, 2013],
       '股本數(億股)': [3791.71, 5218.01, 5875.45, 6428.46, 7149.43, 
                   7629.51, 12683.99, 17000.45, 18900.12, 20606.26, 
                   26984.49, 36095.52, 38395.00, 40569.08]}
df = pd.DataFrame(dic)
df.plot(kind='line', x='年份', y='股本數(億股)', figsize=(8, 6), 
		title='2000~2006年全國股票發行股本數')

plt.show()

在這裏插入圖片描述

2.面積圖(area chart)
又稱區域圖,強調數量隨時間變化的程度。
通常用於引起人們對總值趨勢的注意。
堆積面積圖中,可以直觀的反映部分與整體的關係、各組成部分的變化情況,以及總的變化情況


# 單位:萬人
dic = {'年份': [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
              2009, 2010, 2011, 2012, 2013],
       '0~14歲人口數': [29012, 28716, 28774, 28559, 27947, 26504, 25961, 
                    25660, 25166, 24659, 22259, 22164, 22287, 22315],
       '15~64歲人口數': [88910, 89849, 90302, 90976, 92184, 94197, 95068, 
                     95833, 96680, 97484, 99938, 100283, 100403, 100557],
       '65歲以上人口數': [8821, 9062, 9377, 9692, 9857, 10005, 10419, 10635, 
                    10856, 11307, 11894, 12288, 12714, 13199]}
df = pd.DataFrame(dic)
fig, [ax1, ax2] = plt.subplots(1, 2, sharey=True, figsize=(14, 8)
df.plot(kind='area', ax=ax1, x='年份', stacked=True)
ax1.set_title('2000~2013年年末全國人口數-堆積圖')
df.plot(kind='area', ax=ax2, x='年份', stacked=False)
ax2.set_title('2000~2013年年末全國人口數-面積圖')
plt.show()

在這裏插入圖片描述

3.散點圖
每個點代表一個觀測值,橫縱座標分別代表兩個變量的取值。
散點圖能很好的描述兩個變量之間關係的工具。
散點圖有多種變形,如三維散點圖。還有幾個散點圖畫在一起的,多元迴歸分析中常常用到。

根據我國1981年至2001年的一些宏觀數據,研究國內生產總值(GDP)、農業總產值、社會消費品零售額、進出口額、職工工資總額、固定資產投資額、居民消費水平、城鄉儲蓄存款年額、財政支出總量兩兩之間的關係圖,如圖:
在這裏插入圖片描述

4.對數圖
以時間爲橫軸,以10爲底的對數比率爲縱座標的折線圖。
通常用於反映增長率的變動趨勢
注:由於log101=0log_{10}1=0,所以對數圖的縱座標的刻度是從1開始的。

下圖爲道瓊斯100年的K線圖:
線性圖可以對歷史和現在提供一個整體的宏觀感覺但今天的指數點位與100年前的指數點位沒可比性
在這裏插入圖片描述
下圖爲調整後的對數圖。對數圖能將歷史和現在進行更直觀的對比
在這裏插入圖片描述

  • 附1.增加縱軸單位標識
    測試了幾種增加單位的方法,放到列標籤上應該最佳,其次可據具體的需求直接增加到縱軸上。
    eg.1
    import matplotlib.gridspec as gds
    import matplotlib.ticker as mticker
    
    # 單位:萬人
    dic = {'年份': [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
                  2009, 2010, 2011, 2012, 2013],
           '0~14歲人口數': [29012, 28716, 28774, 28559, 27947, 26504, 25961, 
                        25660, 25166, 24659, 22259, 22164, 22287, 22315],
           '15~64歲人口數': [88910, 89849, 90302, 90976, 92184, 94197, 95068, 
                         95833, 96680, 97484, 99938, 100283, 100403, 100557],
           '65歲以上人口數': [8821, 9062, 9377, 9692, 9857, 10005, 10419, 10635, 
                        10856, 11307, 11894, 12288, 12714, 13199]}
    df = pd.DataFrame(dic)
    
    G = gds.GridSpec(2, 2, wspace=0.35)
    ax1 = plt.subplot(G[0, 0])
    df.plot(ax=ax1, kind='area', x='年份', title='2000~2013年末全國人口數', 
            stacked=True, figsize=(8,8))
    
    ax2 = plt.subplot(G[0, 1])
    df.plot(ax=ax2, kind='area', x='年份', stacked=True)
    plt.ylabel('單位(萬元)')
    
    ax3 = plt.subplot(G[1, 0])
    df.plot(ax=ax3, kind='area', x='年份')
    plt.gca().yaxis.set_major_formatter(mticker.FormatStrFormatter('%s萬人'))
    
    ax4 = plt.subplot(G[1, 1])
    df.plot(ax=ax4, kind='area', x='年份')
    plt.text(2000, 140000, '單位(萬元)')
    
    plt.show()
    
    
    在這裏插入圖片描述
  • 附2. matplotlib.gridspec
help(matplotlib.gridspec)
Help on module matplotlib.gridspec in matplotlib:

NAME
    matplotlib.gridspec

DESCRIPTION
    :mod:`~matplotlib.gridspec` is a module which specifies the location
    of the subplot in the figure.
    
        `GridSpec`
            specifies the geometry of the grid that a subplot will be
            placed. The number of rows and number of columns of the grid
            need to be set. Optionally, the subplot layout parameters
            (e.g., left, right, etc.) can be tuned.
    
        `SubplotSpec`
            specifies the location of the subplot in the given `GridSpec`.

CLASSES
    builtins.object
        GridSpecBase
            GridSpec
            GridSpecFromSubplotSpec
        SubplotSpec
    
    class GridSpec(GridSpecBase)
     |  A class that specifies the geometry of the grid that a subplot
     |  will be placed. The location of grid is determined by similar way
     |  as the SubplotParams.
     |  
     |  Method resolution order:
     |      GridSpec
     |      GridSpecBase
     |      builtins.object
     |  
     |  Methods defined here:
     |  
     |  __getstate__(self)
     |  
     |  __init__(self, nrows, ncols, figure=None, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None, width_ratios=None, height_ratios=None)
     |      The number of rows and number of columns of the grid need to be set.
     |      Optionally, the subplot layout parameters (e.g., left, right, etc.)
     |      can be tuned.
  • 附3.mticker.FormatStrFormatter
>>> help(mticker.FormatStrFormatter)
			 
Help on class FormatStrFormatter in module matplotlib.ticker:

class FormatStrFormatter(Formatter)
 |  Use an old-style ('%' operator) format string to format the tick.
 |  
 |  The format string should have a single variable format (%) in it.
 |  It will be applied to the value (not the position) of the tick.
 |  
 |  Method resolution order:
 |      FormatStrFormatter
 |      Formatter
 |      TickHelper
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __call__(self, x, pos=None)
 |      Return the formatted label string.
 |      
 |      Only the value `x` is formatted. The position is ignored.
 |  
 |  __init__(self, fmt)
 |      Initialize self.  See help(type(self)) for accurate signature.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章