day01_matplotlib_demo

折线图plot

import matplotlib.pyplot as plt

plt.figure(figsize=(15, 6), dpi=80)
plt.plot([1, 0, 9], [4, 5, 6])
plt.show()

在这里插入图片描述

### 展现一周天气温度情况
# 创建画布
plt.figure(figsize=(15, 6), dpi=80)
# 绘制图像
plt.plot([1, 2, 3, 4, 5, 6, 7], [17, 17,  29, 25, 34, 23, 18])
# 显示图像
plt.savefig('test78.jpg')
plt.show()  # 会释放所有的画布资源 所以savefig函数需要放在他的前面

在这里插入图片描述

import matplotlib.pyplot as plt
import random
plt.rcParams['font.sans-serif'] = ['SimHei']  #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  #用来正常显示负号
# 准备数据
x = range(60)
# 这里使用了列表推导式
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 5) for i in x]
# 创建画布
plt.figure(figsize=(10, 4), dpi=80)
# 绘制图像
plt.plot(x, y_shanghai, 'b-.', label='上海')  # label表示显示标签 b-.表示 blue 线条为-.形式
plt.plot(x, y_beijing, 'r--', label='北京')
# 显示图像
plt.legend()
# 修改x y 刻度
plt.yticks(range(0, 40, 5))
x_label = ['11点{}分'.format(i) for i in x]
plt.xticks(x[::5], x_label[::5])
# 添加标题
plt.xlabel('时间变化')
plt.ylabel('温度变化')
plt.title('某城市的11点钟的温度的变化')
# 栅格
plt.grid(True, 'both', 'x')
#显示图像
plt.show()

在这里插入图片描述

多个绘图区

import matplotlib.pyplot as plt
import random
plt.rcParams['font.sans-serif'] = ['SimHei']    #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False      #用来正常显示负号

# 准备数据
x = range(60)
# 这里使用了列表推导式
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 3) for i in x]

figure, axes = plt.subplots(1, 2, figsize=(15, 6), dpi=80)
axes[0].plot(x, y_shanghai, 'r--', label='上海')
axes[1].plot(x, y_beijing, 'b-', label='北京')
# 显示label
axes[0].legend()
axes[1].legend()
# 设置刻度
axes[0].set_yticks(range(0, 40, 5))
axes[0].set_xticks(x[::5], ["11.{} ".format(i) for i in x][::5])
axes[1].set_yticks(range(0, 10, 2))
axes[1].set_xticks(x[::5], ["11.{} ".format(i) for i in x][::5])
# 栅格
axes[0].grid(visible=True, which='both', axis='x')
axes[1].grid(visible=True, which='both', axis='x')
# 设置标题
axes[0].set_xlabel('时间')
axes[0].set_ylabel('温度')
axes[0].set_title('上海天气')
axes[1].set_xlabel('时间')
axes[1].set_ylabel('温度')
axes[1].set_title('北京天气')

plt.show()

在这里插入图片描述

绘制数学函数图像

import matplotlib.pyplot as plt
import numpy as np

# 准备数据
x = np.linspace(-1, 1, 1000)
y = 2 * x * x

# 创建画布
plt.figure(figsize=(15, 6), dpi=80)
# 绘制图像
plt.plot(x, y, 'r-', label='y=2x*x')
plt.legend(loc=1)
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.title('显示函数图像')
# 网格显示
plt.grid(linestyle='--', alpha=0.5)
# 显示图像
plt.show()

在这里插入图片描述

散点图scatter

## 探究房屋面积和房屋价格的关系
import matplotlib.pyplot as plt
import numpy as np

# 准备数据
x = [225.63, 247.07, 253.12, 457.85, 241.58, 303.23, 304.32, 340.34, 220.34, 240.23, 280.45]
y = [196.63, 203.88, 210.75, 374.74, 202.41, 299.35, 300.12, 330.34, 220.34, 222.45, 290.21]

# 创建画布、绘制图像
plt.figure(figsize=(15, 6), dpi=80)
plt.scatter(x, y, alpha=0.5)
plt.show()

在这里插入图片描述

柱状图bar

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']    #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False      #用来正常显示负号

## 对比票房收入
# 准备数据
movie_names = ['雷神3', '正义联盟', '东方快车', '环游记', '全球风暴', '降魔传']
movie_data = [73853, 557767, 22354, 15969, 14839, 8725]
bar_labels = ['red', 'blue', '_red', 'orange', '_bue', '_orange']
bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange', 'tab:blue', 'tab:orange']

# 准备画布、绘制图像
plt.figure(figsize=(15, 6), dpi=80)
bar1 = plt.bar(range(len(movie_names)), movie_data, label=bar_labels, color=bar_colors)
plt.bar_label(bar1, label_type='center')  # 将对应的数据显示在柱状图的中间
plt.legend(title='电影')

# 修改刻度
plt.xticks(range(len(movie_names)), movie_names)

plt.xlabel('电影名称')
plt.ylabel('票房')
plt.title('部分电影票房柱状图')
# 显示图像
plt.show()

在这里插入图片描述

## 对比电影票房收入
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']    #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False      #用来正常显示负号

# 准备数据
movie_name = ['雷神3', '正义联盟', '东方快车']
first_day = [10587.6, 10062.5, 1275.7]
week_day = [36224.9, 34479.6, 11830]
# 创建画布、绘图
figure, axes = plt.subplots(figsize=(15, 6), dpi=80)
p1 = axes.bar(range(len(movie_name)), first_day, label='first_day', width=0.2)
axes.bar_label(p1, label_type='center', color='b')
p2 = axes.bar([0.2, 1.2, 2.2], week_day, label='week_day',  width=0.2)
axes.bar_label(p2, label_type='center', color='r')
axes.legend() 

axes.set_xticks([0.1, 1.1, 2.1], movie_name);

plt.show()


在这里插入图片描述

## 对比电影票房收入
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']    #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False      #用来正常显示负号

# 准备数据
movie_name = ['雷神3', '正义联盟', '东方快车']
first_day = [10587.6, 10062.5, 1275.7]
week_day = [36224.9, 34479.6, 11830]
# 创建画布、绘图
figure, axes = plt.subplots(figsize=(15, 6), dpi=80)
p1 = axes.bar(range(len(movie_name)), first_day, label='first_day', width=0.2, bottom=[0, 0, 0], color=['r', 'g', 'b'])
axes.bar_label(p1, label_type='center', color='w')
p2 = axes.bar(range(len(movie_name)), week_day, label='week_day',  width=0.2, bottom=first_day, color=['b', 'r', 'g'])
axes.bar_label(p2, label_type='center', color='w')
axes.legend() 

axes.set_xticks(range(len(movie_name)), movie_name);

plt.show()

在这里插入图片描述

直方图histogram

## 直方图显示带电影时长的分布状况
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']    #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False      #用来正常显示负号

## 准备数据

movie_time = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, \
     101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111, 78, 132, 124, 113, 150, 110, 117, 86, \
     95, 144, 105, 126, 130, 126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136, 123, 117, 119, 105, 137, \
     123, 128, 125, 104, 109, 134, 125, 127, 105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114, 105, 115, \
     132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134, 156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102, \
     123, 107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133, 112, 114, 122, 109, 106, 123, 116, 131, 127, \
     115, 118, 112, 135, 115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154, 136, 100, 118, 119, 133, 134, \
     106, 129, 126, 110, 111, 109, 141, 120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126, 114, 140, 103, \
     130, 141, 117, 106, 114, 121, 114, 133, 137, 92, 121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113, 134, \
     106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110, 105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, \
     133, 101, 131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111, 111, 133, 150]
## 创建画布、绘图
group_distance = 10  # 组距
num_bins = (max(movie_time)-min(movie_time))//group_distance + 1  # 组数
fig, axes = plt.subplots(figsize=(12, 8), dpi=80)
p = axes.hist(movie_time, num_bins, rwidth=1)
axes.set_xticks(range(min(movie_time), max(movie_time) + group_distance, group_distance))

axes.set_xlabel('影视播放时长分钟', size=16)
axes.set_ylabel('影视个数', size=16)
axes.set_title('影视时间分布直方图', size=18)
plt.grid()  # 绘制网格
## 显示
plt.show()

在这里插入图片描述

饼图pie

## 饼图绘制 显示不同电影的排名
import matplotlib.pyplot as plt

## 准备数据
movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它']
place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105]

## 创建画布、绘制图像
fig, axes = plt.subplots(figsize=(15, 8), dpi=80)
axes.pie(place_count, labels=movie_name, autopct='%1.2f%%', colors=['b', 'r','g','y','c','m','y','k','c','g','g'])
axes.axis('equal')  # 设置显示是圆形
axes.legend(loc='best',  title="Ingredients")
plt.title("排片占比示意图", size=18)
plt.show()

在这里插入图片描述

总结

  • 容器层
    • 画板层Canvas
    • 画布层plt.figure(figsize=(), dpi=)
    • 绘画层/坐标轴fig, axes = plt.subplots(nrows=, ncols=, figsize=(). dpi=)
  • 辅助显示层
    • 修改x,y轴刻度 plt.xticks()
    • 添加描述信息 plt.xlabel() plt.ylabel() plt.title()
    • 添加网格 plt.grid()
    • 显示图例 plt.legend()
  • 图像层,可以设置图像颜色、风格、标签
    • 折线图 plt.plot()
    • 散点图 plt.scatter()
    • 柱状图 plt.bar()
    • 直方图 plt.hist()
    • 饼图 plr.pie()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值