数据分析matplotlib

一 数据分析

  1. 什么是数据分析:

    数据分析是用适当的方法对收集来的大量数据进行分析,帮助人们作出判断,以便采取适当行动

  2. 数据分析的流程
    .在这里插入图片描述

二.matplotlib介绍及使用

2.1 matplotlib介绍
		可以更好的将数据可视化,更直观的呈现,能使数据更具有说服力.
		其是最流行的python底层绘图库.
2.2 matplotlib语法
  • [ 绘制折线图 ]
    from matplotlib import pyplot as plt
    import random
    from matplotlib import font_manager
    
    
    # 设置中文字体
    my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/msyhbd.ttc")
    
    
    # 设置随机种子,使温度每次都相同
    random.seed(10)
    x = range(0, 120)
    y_1 = [random.randint(15, 35) for i in range(120)]
    y_2 = [random.randint(10, 37) for j in range(120)]
    
    # 设置图形大小
    plt.figure(figsize=(15, 8), dpi=80)
    
    plt.plot(x, y_1, label="今天", linestyle="--")
    plt.plot(x, y_2, label="明天", linestyle="-")
    # 设置显示时分
    _x_ticks = ["10时{}分".format(i) for i in x if i < 60]
    _x_ticks += ["11时{}分".format(i - 60) for i in x if i >= 60]
    # x坐标轴的刻度,及描述
    plt.xticks(x[:: 5], _x_ticks[::5], fontproperties=my_font, rotation=45)
    
    # 设置y轴摄氏度
    _y_ticks = ["{}℃".format(j) for j in y_1]
    plt.yticks(y_1, _y_ticks)
    
    # 设置描述信息x轴和y轴代表的意义,并设置字体
    plt.xlabel("时间", fontproperties=my_font)
    plt.ylabel("温度", fontproperties=my_font)
    plt.title("温度变化趋势", fontproperties=my_font)
    
    # 添加图例,loc=0代表最佳位置
    plt.legend(prop=my_font, loc=0)
    
    # 保存图片
    plt.savefig("./wendu.jpg")
    # 显示图片
    plt.show()

在这里插入图片描述

  • [ 绘制散点图 ]
from matplotlib import pyplot as plt
from matplotlib import font_manager


# 设置中文字体
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/msyhbd.ttc")

a = [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]
b = [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]

x_3 = range(1, 32)
x_10 = range(51, 82)

# 设置图像大小
plt.figure(figsize=(20, 8), dpi=80)

# 绘制散点图
plt.scatter(x_3, a, label="三月份")
plt.scatter(x_10, b, label="十月份")

# 修改横纵坐标
_x = list(x_3) + list(x_10)
_xticks = ["三月{}日".format(i) for i in x_3]
_xticks += ["十月{}日".format(i - 50) for i in x_10]
plt.xticks(_x[::5], _xticks[::5], fontproperties=my_font)
#
# 设置坐标含义
plt.ylabel("温度", fontproperties=my_font)
plt.xlabel("月份", fontproperties=my_font)
plt.title("温度散点图", fontproperties=my_font)

# 添加图例
plt.legend(prop=my_font, loc=0)

# 保存图片
plt.savefig("./scatter.jpg")
# 显示散点图
plt.show()

在这里插入图片描述

  • [ 绘制条形图 ]
from matplotlib import pyplot as plt
from matplotlib import font_manager


# 设置字体
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/msyhbd.ttc")

# 数据
a = ["战狼2", "速度与激情8", "功夫瑜伽", "西游伏妖篇", "变形金刚5:最后的骑士",
    "摔跤吧!爸爸", "加勒比海盗5:死无对证", "金刚:骷髅岛", "极限特工:终极回归",
    "生化危机6:终章", "乘风破浪", "神偷奶爸3", "智取威虎山", "大闹天竺",
    "金刚狼3:殊死一战", "蜘蛛侠:英雄归来", "悟空传", "银河护卫队2", "情圣",
    "新木乃伊"]

b = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3,
    8.75, 7.55, 7.32, 6.99, 6.88, 6.86, 6.58, 6.23]

# 设置图片大小
plt.figure(figsize=(20, 16), dpi=82)
# 计算数据长度
_x = range(len(a))

# 绘制条形图(横向),其宽度0.3,颜色orange
plt.bar(_x, b, width=0.3, color="orange")
# 竖向 plt.barh(_x, b, height=0.3)
plt.xticks(_x, a, fontproperties=my_font, rotation=90)


# 添加网格
plt.grid(alpha=0.5)

plt.show()




在这里插入图片描述

  • [ 绘制直方图 ]
import pandas as pd
from matplotlib import pyplot as plt


# 读取csv文件
movie_data = pd.read_csv("IMDB-Movie-Data.csv")
# print(movie_data.info())

# rating,runtime的分布情况
runtime_data = movie_data["Runtime (Minutes)"].values
print(runtime_data)
print("*" * 100)
# 求出runtime的最大值和最小值
runtime_max = runtime_data.max()
runtime_min = runtime_data.min()
print(runtime_max, runtime_min)

d = 5
# 计算直方图坐标划分个数,最好是整数,不然会错位
num_bin = (runtime_max - runtime_min) // d
print(num_bin)

# 绘制直方图
plt.figure(figsize=(20, 8), dpi=80)
plt.hist(runtime_data, num_bin)

plt.xticks(range(runtime_min, runtime_max+d, d))

# 设置x轴和y轴的描述
plt.xlabel("running time")
plt.ylabel("statistic number")
plt.title("movie running time data")

plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值