Python 数据分析2:Matplotlib 散点图、条形图、直方图

1. 绘制散点图

plt.scatter() 显示散点图

import matplotlib
from matplotlib import pyplot as plt

# 设置字体
font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
# 设置图片大小
plt.figure(figsize=(20, 8), dpi=80)
x_3 = range(1, 32)
x_10 = range(51, 82)
y_3 = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15, 15, 19, 21, 22, 22,
       22, 23]
y_10 = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 18, 22, 23, 17, 20, 21, 20, 22, 15, 11, 15, 5, 17, 13, 10, 11, 13,
        12, 13, 6]
# 绘制散点
plt.scatter(x_3, y_3)
plt.scatter(x_10, y_10)
# 设置x轴刻度文字
_x = list(x_3) + list(x_10)
_xtick_labels = [f"3月{i}日" for i in x_3]
_xtick_labels += [f"10月{i}日" for i in x_10]
plt.xticks(_x, _xtick_labels, rotation=90)
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("某市气温变化")
plt.show()

 2. 绘制条形图 电影票房案例

plt.bar() 绘制条形图

import matplotlib
from matplotlib import pyplot as plt

font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
plt.figure(figsize=(20, 14), dpi=80)
x_film_title = ["阿凡达", "复仇者联盟4", "泰坦尼克号", "星球大战7", "复仇者联盟3", "狮子王(2019)", "侏罗纪世界", "复仇者联盟1", "速度与激情7", "冰雪奇缘2"]
y_box_office = [2847.2, 2797.5, 2201.6, 2069.5, 2048.3, 1670.7, 1670.5, 1518.8, 1515.3, 1450.0]
plt.bar(x_film_title, y_box_office)
_ytick_labels = [f"${i}M" for i in range(14, 30)]
plt.xticks(rotation=45)
plt.xlabel("电影名称")
plt.ylabel("票房(百万美元)")
plt.title("全球电影最高票房纪录")
plt.show()

 3. 多个条形图

import matplotlib
from matplotlib import pyplot as plt

font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
plt.figure(figsize=(20, 14), dpi=80)
film_title = ["你好,李焕英", "唐人街探案3", "刺杀小说家", "人潮汹涌"]
x_date = ["2021-2-12", "2021-2-13", "2021-2-14", "2021-2-15"]
y_12 = [29146, 101117, 13742, 4321]
y_13 = [30239, 81826, 8316, 1553]
y_14 = [45574, 74971, 9351, 1978]
y_15 = [53754, 43634, 7604, 1761]
bar_width = 0.2
# 设置x坐标偏移量 每个柱宽度为0.2
x_1 = list(range(len(film_title)))
x_2 = [i + bar_width for i in x_1]
x_3 = [i + bar_width for i in x_2]
x_4 = [i + bar_width for i in x_3]
x_5 = [i + bar_width for i in x_4]
plt.bar(x_1, y_12, width=bar_width, label=x_date[0])
plt.bar(x_2, y_13, width=bar_width, label=x_date[1])
plt.bar(x_3, y_14, width=bar_width, label=x_date[2])
plt.bar(x_4, y_15, width=bar_width, label=x_date[3])
plt.xticks(x_3, film_title, fontsize=20)
plt.yticks(fontsize=20)
plt.ylabel("票房(万元)")
plt.title("2021春节档电影票房")
plt.legend()
plt.show()

 4. 直方图

import matplotlib
from matplotlib import pyplot as plt

font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
plt.figure(figsize=(20, 14), dpi=80)
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, 137, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111, 78, 132, 124, 113, 150, 110,
        117, 86, 95, 124, 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, 102, 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, 135, 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, 134, 151]
# 组距
d = 3
# 组数
num_bins = (max(time) - min(time)) // d
# 频数分布直方图
plt.hist(time, num_bins)
# 频率分布直方图
# plt.hist(a, num_bins,normed=True)
# 设置x轴的刻度
plt.xticks(range(min(time), max(time) + d, d))
plt.grid()
plt.show()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hinomoto Oniko

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值