數據可視化(數學建模用到的畫圖)

 

01柱狀圖

from pylab import *  # 這兩行代碼,專門用來顯示漢子問題

mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei']  # 這兩行代碼,專門用來顯示漢子問題

x1 = np.arange(2004, 2017)
y1 = [970279, 1259308, 1127571, 1163959, 1169540, 1076938, 991350, 953275, 951508, 904434, 889381, 864015, 836236, ]
y1 = np.array(y1)
# y2 = [1435, 3402, 3339, 3669, 2802, 3783, 3000, 2840, 2662, 2576, 2240, 2280, 2465]

plt.figure(1)
plt.bar(x1, y1 / 10000, 0.6, color="coral")
plt.title("發病人數與時間的關係")
plt.xlabel(u"年份")
plt.ylabel(u"發病人數")
plt.text(2001.5, 127, "單位:萬人")  # 在圖表任何位置添加一些註釋或者說明
plt.show()

02折線圖

# """
# @author:Zhao
# @ide:PyCharm
# @createTime:2019-05-26
# """
import numpy as np
from pylab import *  # 這兩行代碼,專門用來顯示漢子問題

plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']  # 這兩行代碼,專門用來顯示漢子問題

x1 = np.arange(2004, 2017)
x2 = np.arange(2004, 2020)

y1 = [970279, 1259308, 1127571, 1163959, 1169540, 1076938, 991350, 953275, 951508, 904434, 889381, 864015, 836236, ]
y1 = np.array(y1)
y2 = [1041613.1, 1144841.3, 1148142.4, 1155784.4, 1163656.7, 1120077.3, 1049225.1, 992511.8, 965732.8, 931802.3,
      906496.6, 882183.2, 856137.6, 807201.4, 776392.1, 743850.4]
y2 = np.array(y2)
y3 = [970280, 1228788, 1187444, 1146911, 1107177, 1068200, 1029999, 992514, 955770, 919738, 884407, 849761, 815788,
      749800, 717800, 686360]
y3 = np.array(y3)

plt.figure(1)

plt.plot(x1, y1 / 10000, label='實際發病人數', c='r')
plt.scatter(x1, y1 / 10000, c='r')
plt.plot(x2, y2 / 10000, label='三次指數平滑預測發病人數')
plt.scatter(x2, y2 / 10000)
plt.plot(x2, y3 / 10000, label='灰色模型預測發病人數', c='g')
plt.scatter(x2, y3 / 10000, c='g')
plt.title("發病人數與時間的變化趨勢")
plt.xlabel(u"年份")
plt.ylabel(u"發病人數")
plt.legend()
plt.xticks(range(2004, 2021, 2))
plt.text(2001, 127, "單位:萬人")

plt.show()

03餅狀圖

import pandas as pd
import numpy as np
from collections import Counter
from pylab import *  # 這兩行代碼,專門用來顯示漢子問題

mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei']  # 這兩行代碼,專門用來顯示漢子問題

import heapq


def plot_pie(x, y):
    colors = 'yellowgreen', 'gold', 'lightskyblue', 'lightcoral', 'red', 'orange', 'blue', 'yellow', 'green',
    explode = 0, 0, 0, 0, 0, 0, 0, 0, 0,
    plt.pie(y, explode=explode, labels=x, colors=colors, autopct='%1.1f%%', shadow=False, startangle=50)
    plt.axis('equal')
    plt.show()


if __name__ == '__main__':
    result = [586320., 88228., 50480., 44760., 40888., 32516., 24806., 22952., 47127]

    x = '農民', '家務及待業', '工人', '學生', '離退人員', '其餘', '民工', '不詳', '其他職業'
    plot_pie(x, result)

04雙座標軸的折線圖

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc

rc('mathtext', default='regular')
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']  # 這兩行代碼,專門用來顯示漢子問題
guangDong = [5664.37, 9485.99, 16153.25, 24834.65, 31551.37]
heNan = [5633.24, 9333.4, 15967.61, 24791.83, 32665.38]
siChuan = [1677.8, 2884.11, 4602.16, 8086.86, 11776.73]
guangDong = np.array(guangDong)
heNan = np.array(heNan)
siChuan = np.array(siChuan)
guangDongFB = [42883.0, 66049.0, 56753.0, 59090.0, 51157.0]
heNanFB = [59744.0, 64005.0, 48479.0, 48045.0, 43712.0]
siChuanFB = [46728.0, 63842.0, 49049.0, 46783.0, 46117.0]
guangDongFB = np.array(guangDongFB)
heNanFB = np.array(heNanFB)
siChuanFB = np.array(siChuanFB)
x = np.arange(2004, 2017, 3)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, guangDong / 10000, c='red', label='湖南GDP')
ax.plot(x, heNan / 10000, c='blue', label='湖北GDP')
ax.plot(x, siChuan / 10000, c='orange', label='貴州GDP')
ax2 = ax.twinx()
ax2.plot(x, guangDongFB / 10000, '--', c="red", label='湖南發病人數')
ax2.plot(x, heNanFB / 10000, '--', c="blue", label='湖北發病人數')
ax2.plot(x, siChuanFB / 10000, '--', c="orange", label='貴州發病人數')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("時間")
ax.set_ylabel("GDP")
ax2.set_ylabel("發病人數")
# ax.set_ylim(-20, 100)
ax2.legend(loc=0)
# plt.savefig('0.png')
plt.text(2001.5, 6.65, "單位:萬億元")
plt.text(2016.6, 6.65, "單位:萬人")
plt.show()

相關文章
相關標籤/搜索