Matplotlib基础

 一、简单介绍

mat - matrix矩阵
plot - 画图
lib - library库
 

import matplotlib.pyplot as plt
%matplotlib inline

plt.figure()  
plt.plot([1, 0, 9], [4, 5, 6])
plt.show()

1. 三层结构

- 容器层:画板层(Canvas)、画布层(plt.figure)、绘图层/坐标系(plt.subplots)
- 辅助显示层
- 图像层

二、绘图

1. 折线图(plot)

- figsize:指定图的长度
- dpi:图像的清晰度
- alpha:透明度
- loc:图例位置

# 展示天气

# 绘制画布
plt.figure(figsize=(20, 8), dpi=80)
# 绘制图像
plt.plot([1, 2, 3, 4, 5, 6, 7], [17, 18, 15, 13, 11, 11, 13])
# 保存图像
plt.savefig("C:/Users/黎明杀鸡槑/Desktop/test.png")
# 显示图像
plt.show()

                              

# 11-12点每分钟温度变化
import random
# 准备数据x y
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
plt.figure(figsize=(20, 8), dpi=100)
plt.plot(x, y_shanghai)
# 修改x y 刻度
# 准备x的刻度说明
x_label = ["11点{}分".format(i) for i in x]
plt.xticks(range(0, 60, 5),  x_label[::5])
plt.yticks(range(0, 40, 5))
# 添加网格显示
plt.grid(True, linestyle='--', alpha=0.5)
# 添加描述信息
plt.xlabel('时间变化')
plt.ylabel('温度变化')
plt.title('11-12点温度变化')
# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
plt.show()

                               

# 添加一个城市的温度
import random
x = range(60)
y_beijing = [random.uniform(1, 3) for i in x]
y_shanghai = [random.uniform(15, 18) for i in x]
plt.figure(figsize=(20, 8), dpi=100)
plt.plot(x, y_shanghai, color='r', linestyle='-.',label='上海')
plt.plot(x, y_beijing,color='g',label='北京')
# 显示图例
plt.legend(loc=0)
# 修改x y 刻度
# 准备x的刻度说明
x_label = ["11点{}分".format(i) for i in x]
plt.xticks(range(0, 60, 5),  x_label[::5])
plt.yticks(range(0, 40, 5))
# 添加网格显示
plt.grid(True, linestyle='--', alpha=0.5)
# 添加描述信息
plt.xlabel('时间变化')
plt.ylabel('温度变化')
plt.title('上海、北京11-12点温度变化')
# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
plt.show()

                                         

import matplotlib.pyplot as plt
import random
# 1.准备数据
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 3) for i in x]
# 2.创建画布
# plt.figure(figsize=(10, 4), dpi=80)
figure, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 4), dpi=80)
# 3.绘制图像
axes[0].plot(x, y_shanghai, color="r", linestyle="--",label="上海")
axes[1].plot(x, y_beijing, color="b", label="北京")
# 显示图列
axes[0].legend()
axes[1].legend()
# 4.添加自定义x,y
x_label = ["11点{}分".format(i) for i in x]
axes[0].set_xticks(x[::5])
axes[0].set_xticklabels(x_label[::5])
axes[0].set_yticks(range(0, 40, 5))
axes[1].set_xticks(x[::5])
axes[1].set_xticklabels(x_label[::5])
axes[1].set_yticks(range(0, 40, 5))
# 5 添加网络显示
axes[0].grid(True, linestyle="--", alpha=0.5)
axes[1].grid(True, linestyle="--", alpha=0.5)
# 6.添加描述信息
axes[0].set_xlabel("时间")
axes[0].set_ylabel("温度")
axes[0].set_title("上海中午11点0分到12点之间的温度变化")
axes[1].set_xlabel("时间")
axes[1].set_ylabel("温度")
axes[1].set_title("北京中午11点0分到12点之间的温度变化")
# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
# 7.显示图
plt.show()

# 绘制数学函数图像
import numpy as np
x = np.linspace(-1, 1, 1000)
y = 2*x*x
plt.figure(figsize=(20, 8), dpi=90)
plt.plot(x,y)
plt.grid(linestyle='--',alpha=0.5)
plt.show()

                                                      

2. 散点图(scatter)

-- 探究不同变量之间的关系/规律 

# 天就房屋面积和房屋价格的关系

x = [225.98, 247.8, 153.14, 457.85, 241.58, 302.14, 60.34, 288.54, 164.46,
     120.23, 207.4, 345.1, 147.42, 53.05]
y = [196.23, 204.2, 210.74, 372.73, 202.41, 247.53, 69.3, 239.32, 140.23,
    104.24, 176.68, 288.25, 128.47, 49.4]

plt.figure(figsize=(18, 8), dpi=80)
plt.scatter(x, y)
plt.show()

                                       

3.柱状图(bar)

-- 数量统计
-- 用户数量对比分析

# 对比每部电影的票房收入

movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕']
tickets = [73853, 57767, 22354, 15969, 14839, 8725, 8716]
plt.figure(figsize=(20,8), dpi=90)
plt.bar(range(len(movie_name)),tickets, color=['b', 'r', 'g', 'y', 'c', 'm', 'k'])
plt.xticks(range(len(movie_name)), movie_name)
plt.title("电影票房收入对比")
plt.grid(linestyle='--', alpha=0.5)
# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
plt.show()

                                        


movie_name = ['雷神3:诸神黄昏','正义联盟','寻梦环游记']
first_day = [10587.6, 10062.5, 1275.7]
first_weekend = [36224.9, 34479.6, 11830]
a = range(len(move_name))
plt.figure(figsize=(20, 8), dpi=80)
plt.bar(a , first_day, width=0.2,label="首日票房")
plt.bar([i + 0.2 for i in a ], first_weekend,width=0.2, label='首周票房')
plt.legend()
plt.xticks([i + 0.1 for i in a ],movie_name)
plt.show()

                               

4.直方图(histogram)

-- 分布状况

# 电影时长分布
time = [131,98,125,131,124,91,119,117,94,126,109,120,108,96,121,127,97,102,107,114,107,106,96,126,102,104,104,123,107,
        124,113,100,102,116,101,105,89,102,111,103,92,126,110,112,92,115,108,128,101,129,93,96,121,115,118,99,116,103,103,119,102,98,112,93,93,94,113,115,116]
plt.figure(figsize=(20, 8), dpi=80)
distance=2
group_num = (max(time)-min(time))
plt.hist(time, bins=group_num)
plt.xticks(range(min(time), max(time), distance))
plt.grid(linestyle='--', alpha=0.5)
plt.show()

                            

5.饼图(pie)

-- 占比
-- autopct:占比显示指定%1.2f%%

# 电影排片占比
movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕']
place_count = [60605,54546,45819,28243,13270,9945,7679]
plt.figure(figsize=(20, 8), dpi=80)

plt.pie(place_count, labels=movie_name, colors=['b', 'r', 'g', 'y', 'c', 'm', 'k'], autopct='%1.2f%%')
plt.axis("equal")
plt.legend()
plt.show()

                                                 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值