数据分析常用库——matplotlib

一个小白想记录一下自己学习的内容,在科研路上越走越远。

为什么要学习数据分析:数据分析是python数据科学的基础,是机器学习课程的基础,坦白说即为对数据进行分析。

matplotlib:将数据进行可视化,数据更加客观和具有说服力。
axis轴:指的是x或y这样的坐标轴

绘制折线图实例:

from matplotlib import pyplot as plt

# 数据在x/y轴的位置,为可迭代对象,数据一起组成了要绘制的坐标
x = range(2,26,2)
y = [15, 13, 14.5, 17, 20, 25, 26, 27, 24, 22, 18, 15]

# 图形图标实例化一个figure能够传递参数,dpi让图片更加清晰
fig = plt.figure(figsize=(20, 8), dpi=80)

# 传入x/y,通过plot绘制出折线图
plt.plot(x, y)

# 调整x或者y轴的刻度,刻度密集时可以用x[::2]调整步长来解决.
plt.xticks(x[::2])
plt.yticks(y)

# 亦可保存为svg这种矢量图格式,放大不会有锯齿
plt.savefig("./sig_size.png")
# 展示图片
plt.show()

感觉最麻烦的就是对于数据的调整,比如下面:

import random
from matplotlib import pyplot as plt
import matplotlib
# 设置在图形中显示中文,正常不显示中文
matplotlib.rc("font", family='MicroSoft YaHei', weight='bold')

fig = plt.figure(figsize=(20, 8))
x = range(0, 120)
# 随机生成20-35的列表
y = [random.randint(20, 35) for i in range(0, 120)]

plt.plot(x, y)

_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i-60) for i in range(60, 120)]
# 注意 range生成的为一元组,将其转换成list类型
plt.xticks(list(x)[::5], _xtick_labels[::5], rotation=45)
# 让列表x中的数据和_x_ticks上的数据都传入,最终会在x轴上一一显示
# 两组数据的长度必须一样,否则不能完全覆盖整个轴
# 使用列表切片,每隔5个选一个数据进行展示

# 网格/x/y轴标签及标题设置
plt.grid(alpha=0.4)
plt.xlabel("时间")
plt.ylabel("温度(摄氏度)")
plt.title("10点到12点每分钟的气温变化情况")
plt.show()

自定义图形风格:plt.plot(x, y, color=‘r’, linestyle=’–’, linewidth=5, alpha=0.5) 自定义线条颜色/线条风格/线条宽度/透明度

为线条添加标签:plt.plot(x, y, label=“曲线”) plt.legend(loc=“best”) 将标签显示在图形中 loc 指定显示位置

matplotlib绘制不同统计图:(统计图选择)

  • 折线图:表示统计数量的增减变化的统计图(显示数据的变化趋势,反映事物的变化情况)
  • 直方图:绘制连续型的数据,展示一组或者多组数据的分布状况(一般横轴表示数据范围,纵轴表示分布情况)
  • 条形图:绘制离散的数据,清晰比较数据之间的差别(排列在工作表的列和行中的数据绘制到条形图中)
  • 散点图:两组数据构成多个坐标点,用于判断变量之间是否存在数量关联趋势

绘制散点图实例:

import matplotlib.pyplot as plt
import matplotlib

matplotlib.rc("font", family='MicroSoft YaHei', weight='bold')

fig = plt.figure(figsize=(18, 10), dpi=80)
x1 = [i+1 for i in range(31)]
x2 = [i+40 for i in range(31)]

month1 = ["3月{}日".format(i+1) for i in range(31)]
month2 = ["11月{}日".format(i+1) for i in range(31)]

tem1 = [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]
tem2 = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23,
        17, 20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11, 13, 12, 13, 6]

month = month1+month2
x = x1+x2

plt.scatter(x1, tem1, label="3月份")
plt.scatter(x2, tem2, label="11月份")
plt.xticks(x[::3], month[::3], rotation=45)

plt.xlabel("月份")
plt.ylabel("气温(摄氏度)")
plt.title("3月11月气温变化")
# legend()函数显示图形元素
plt.legend()

plt.show()

绘制条形图实例:

# 电影条形图日期票房对比
from matplotlib import pyplot as plt
import matplotlib

matplotlib.rc("font", family="MicroSoft YaHei", weight="bold")

movie = ["猩球崛起3:终极之战", "敦刻尔克", "蜘蛛侠:英雄归来", "战狼2"]
day_1 = [15746, 312, 4497, 319]
day_2 = [12357, 156, 2045, 168]
day_3 = [2358, 399, 2358, 362]

x = list(range(len(movie)))
# 注意对于width的调整
x_2 = [i + 0.1 for i in x]
x_3 = [i + 0.1*2 for i in x]

fig = plt.figure(figsize=(15, 8), dpi=80)
plt.bar(x, day_1, label="9月1日", width=0.1)
plt.bar(x_2, day_2, label="9月2日", width=0.1)
plt.bar(x_3, day_3, label="9月3日", width=0.1)
# 设置标签和网格
plt.xticks(x_2, movie)
plt.grid(alpha=0.3)
plt.xlabel("电影名称")
plt.ylabel("票房:亿")

plt.legend()

plt.show()

绘制柱状(直方图)图实例:

注意: 对数据进行分组:组数要适当,太少会有较大统计误差,太多规律不明显
一般来说:组数=极差/组距=(max(a)-min(a))/width

# 频数分布直方图
from matplotlib import pyplot as plt
import matplotlib

matplotlib.rc("font", family="MicroSoft YaHei")

timeLong = [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]

# 设置组距为3
bin_width = 3
# 分为多少组 num_bins须为整数,即可以整除
num_bins = int((max(timeLong)-min(timeLong))/bin_width)

# 频率直方图:plt.hist(timeLong, num_bins, normed=True)
# 频数直方图
plt.hist(timeLong, num_bins)
plt.xticks(list(range(min(timeLong), max(timeLong)+bin_width))[::bin_width])
plt.grid(alpha=0.3)
plt.xlabel("时长(分钟)")
plt.ylabel("数量")
plt.title("直方图")


plt.show()

ps:数据分析这部分是根据黑马视频写的,也不知道有没有侵权…大家一起学习,有需要资源的朋友可以留言
剩下的再发现的问题,随时补充吧

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值