Matplotlib
什么是 matplotlib
matplotlib是最流行的Python底层绘画库,主要用于数据可视化图表。
matplotlib可以将数据进行可视化,更直观的呈现,使数据更加客观,更加具有说服力
Matplotlib 常用设置
设置图片大小
plt.figure(figsize=(20, 8), dpi=100)
dpi参数可以使图片更加清晰
调整 x 轴或者 y 轴上的刻度
plt.xticks(x[:]) plt.yticks(range(min(y), max(y)+1)) plt.xlabel("时间", fontproperties=myfont) plt.ylabel("温度(摄氏度)", fontproperties=myfont) plt.title("10点到12点每分钟时间的时间变化情况", fontproperties=titlefont) plt.show()
通过ticks来设置刻度,label来设置含义
设置中文显示
# fc-list :lang=zh =====> 列出电脑上所有的中文字体库 myfont = font_manager.FontProperties(fname="/usr/share/fonts/wqy-microhei/wqy-microhei.ttc", size=12) titlefont = font_manager.FontProperties(fname="/usr/share/fonts/wqy-microhei/wqy-microhei.ttc", size=32, weight=True)
散点图
import random from matplotlib import pyplot as plt from matplotlib.font_manager import FontProperties """ 绘制10月和三月温度变化的散点图 """ # 字体设置 myfont = FontProperties(fname="/usr/share/fonts/wqy-microhei/wqy-microhei.ttc", size=14) titlefont = FontProperties(fname="/usr/share/fonts/wqy-microhei/wqy-microhei.ttc", size=25) # x轴和Y轴数据 march_y = [random.randint(10, 20) for i in range(31)] october_y = [random.randint(20, 30) for j in range(31)] x = range(1, 32) # 1, 2, 3, ...31 # 设置图形大小 plt.figure(figsize=(10, 5), dpi=100) # 绘制散点图 plt.subplot(2, 1, 1) plt.scatter(x, march_y, ) # 调整刻度 _x3_labels = ["3月{}日".format(_) for _ in x] # rotation: 旋转 plt.xticks(x[::3], labels=_x3_labels[::3], fontproperties=myfont, rotation=45) # x, y和标题的说明 plt.xlabel("月份", fontproperties=myfont) plt.ylabel("温度(摄氏度)", fontproperties=myfont) plt.title("3月温度变化散点图", fontproperties=titlefont) plt.subplot(2, 1, 2) plt.scatter(x, october_y) _x10_labels = ["10月{}日".format(_) for _ in x] plt.xticks(x[::3], labels=_x10_labels[::3], fontproperties=myfont, rotation=45) # x, y和标题的说明 plt.xlabel("月份", fontproperties=myfont) plt.ylabel("温度(摄氏度)", fontproperties=myfont) plt.title("10月温度变化散点图", fontproperties=titlefont) # 添加网格 plt.grid(alpha=0.5) # 显示图像 plt.show()
散点图的应用场景
条形图
单个条形图绘制
""" bar(): pyplot 子模块提供 bar() 函数来生成条形图。 """ from matplotlib import pyplot as plt x = [5, 8, 10] y = [12, 16, 6] x2 = [6, 9, 11] y2 = [6, 15, 7] plt.bar(x, y, align='center') plt.bar(x2, y2, color='g', align='center') plt.title('Bar graph') plt.ylabel('Y axis') plt.xlabel('X axis') plt.show()
多个条形图绘制
import pandas as pd import numpy as np from matplotlib import pyplot as plt df = pd.read_csv('douban.csv') # 获取评分的区间 counts = {} scores = df['score'] for score in scores: try: # 'score' score = float(score.strip()) except Exception as e: continue if 9.5 < score <= 10: counts['9.5~10'] = counts.get('9.5~10', 0) + 1 elif 9.0 < score <= 9.5: counts['9.0~9.5'] = counts.get('9.0~9.5', 0) + 1 elif 8.7 < score <= 9.0: counts['8.7~9.0'] = counts.get('8.7~9.0', 0) + 1 elif 8.5 < score <= 8.7: counts['8.5~8.7'] = counts.get('8.5~8.7', 0) + 1 else: counts['other'] = counts.get('other', 0) plt.bar(counts.keys(), counts.values(), color='g') plt.show()
直方图绘制
import random import matplotlib.pyplot as plt names = ['电影%s' %(i) for i in range(100)] times = [random.randint(40, 140) for i in range(100)] # hist代表直方图, times为需要统计的数据, 20为统计的组数. plt.hist(times, 20) plt.show()