用Python生成柱狀圖、折線圖、餅狀圖來統計本身的手機話費

環境

  • macOS
  • PyCharm

依賴庫

  • matplotlib
  • numpy

中文字體

步驟詳解

1、配置依賴包

使用pycharm,直接依賴 matplotlib,若是當前環境變量的pip中不包含 matplotlib 庫則會報錯,根據IDE提示自動下載 matplotlib 庫
import matplotlib

2、配置中文字體

下載中文字體文件 [SimHei](https://www.fontpalace.com/font-details/SimHei/) 並雙擊則安裝到操做系統的字體庫,而後輸入一下代碼找到 matplotlib 庫的字體目錄

import matplotlib
print(matplotlib.matplotlib_fname())
  1. 參考輸出`/usr/local/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc`
  2. 打開finder(訪達)並按command+shift+g
  3. 輸入上面獲得的路徑並前往
  4. 找到 matplotlib 字體文件庫,如`/usr/local/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf`
  5. 將下載的SimHei.ttf文件拷貝到這裏
  6. 找到字體配置文件 matplotlibrc 可能在`/usr/local/lib/python3.7/site-packages/matplotlib/mpl-data/`目錄下
  7. 修改其中的如下三項
  8. 從新加載字體配置使新增字體生效
matplotlibrc配置
font.family : sans-serif
font.sans-serif : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
axes.unicode_minus:False

從新加載字體配置
from matplotlib.font_manager import _rebuild
_rebuild()

3、準備數據源

mobile_176xxxx4617 = [6.33, 24.1, 40.7, 47.9, 31.9, 31.4, 41.7, 46.4, 38.9, 39, 48, 47.4]
mobile_155xxxx9617 = [41.4, 36.5, 16.4, 16.6, 16.1, 16, 16, 31.2, 20, 16, 22.1, 16]
mobile_173xxxx9636 = [9.4, 9.1, 9.7, 9.1, 9.2, 9, 9, 9.1, 39, 9, 19.22, 19]
time = ['2018.4', '2018.5', '2018.6', '2018.7', '2018.8', '2018.9', '2018.10', '2018.11', '2018.12', '2019.1', '2019.2', '2019.3']

 4、生成統計圖

  • 柱狀圖 module_histogram.py
  • 折線圖 module_line_graph.py
  • 餅狀圖 module_pie_chart.py

 5、效果預覽圖

柱狀圖

折線圖
python

餅狀圖git


項目地址github

相關文章
相關標籤/搜索