Matplotlib 是Python 2D绘图领域的基础套件,它让使用者将数据图形化,并提供多样化的输出格式。这里将会以四个小案例探索Matplotlib的常见用法
绘制折线图
import matplotlib.pyplot as plt
import random
# 保证生成的图片在浏览器内显示
%matplotlib inline
# 保证能正常显示中文(Mac)
plt.rcParams['font.family'] = ['Arial Unicode MS']
# 模拟海南一天的温度变化
# 生成x轴的24小时
hainan_x = [h for h in range(0, 24)]
# 生成y轴的温度随机值(15, 25)
hainan_y = [random.randint(15, 25) for t in range(0, 24)]
# 设置画板属性
plt.figure(figsize = (10, 8), dpi = 100)
# 往画板绘图
plt.plot(hainan_x, hainan_y, label="海南")
# 模拟北京一天内温度的变化
# 生成x轴的24小时
beijing_x = [h for h in range(0, 24)]
# 生成y轴的温度随机值(5, 10)
beijing_y = [random.randint(5, 10) for t in range(0, 24)]
# 往画板绘图
plt.plot(beijing_x, beijing_y, label="北京")
# 模拟河北一天内温度的变化
hebei_x = beijing_x
hebei_y = [random.randint(1, 5) for t in range(0, 24)]
# 自定义绘制属性: 颜色color="#0c8ac5", linestyle"-"""--""-.":", 线宽linewidth, 透明度alpha
plt.plot(hebei_x, hebe