数据分析之matplotlib

一.折线图(某天10点到11点的温度变化情况)

from matplotlib import pyplot as plt
from matplotlib import font_manager
import random
# 找到中文字体存放路径及名称
my_font = font_manager.FontProperties(fname='C:WindowsFontsSTFANGSO.TTF')
# 设置x轴
x = range(0, 120)
# 设置y轴
y = [random.randint(20, 35)for i in range(120)]
# 设置大小及清晰程度
plt.figure(figsize=(20, 8), dpi=80)
# 合并想x,y轴
plt.plot(x, y)
# 设置x轴刻度大小
_x = list(x)
_x_detail = ['10点{}分'.format(i)for i in range(60)]
_x_detail += ['11点{}分'.format(i) for i in range(60)]
plt.xticks(_x[::3], _x_detail[::3], rotation=45, fontproperties=my_font)  # rotation旋转度数
# 设置x,y轴所代表的含义以及单位
plt.xlabel('时间/(分钟)', fontproperties=my_font)
plt.ylabel('温度/(摄氏度)', fontproperties=my_font)
# 整个图的含义
plt.title('10.28 10点到11点的温度变化', fontproperties=my_font)

# 保存图片
plt.savefig('./img/6.png')
# # 展示图片
# plt.show()

2.一张表中展示多条折线图(两个人在近几年来,交朋友的情况)

"""
近年来两人交男女朋友的情况
"""
from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname='C:WindowsFontsSTFANGSO.TTF')
x = range(11, 31)
# 自己
y1 = [1, 0, 1, 1, 2, 6, 3, 2, 3, 4, 2, 3, 1, 5, 6, 4, 3, 2, 0, 1]
# 朋友
y2 = [0, 3, 2, 1, 2, 3, 4, 2, 3, 1, 3, 5, 3, 2, 3, 0, 0, 0, 2, 1]
# 设置大小
plt.figure(figsize=(20,8),dpi=80)
# 设置x,y刻度
plt.xticks(x)
plt.yticks(y1)
plt.yticks(y2)
# 画图
plt.plot(x,y1,label='自己')
plt.plot(x,y2,label='朋友')
# 添加网格,以及调节透明度
plt.grid(alpha=0.2)
# 添加图例
plt.legend(prop=my_font,)
plt.savefig('./img/8.png')

3.散点图(plt.scatter)

"""
散点图表示4月份和12月份的温度
"""

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname='C:WindowsFontssimhei.ttf')
plt.figure(figsize=(20,8),dpi=80)
# y轴温度
y1 = [23, 14, 35, 6, 13, 34, 12, 23, 5, 23, 12, 23, 21, 11, 12]
y2 = [3, 4, 1, 5, 6, 3, 12, 3, 12, 2, 2, 3, 20, 12, 16]
# 4月份x轴,12月x轴
x1 = range(1,16)
x2 = range(30,45)
# 画图
plt.scatter(x1,y1,label='4月份')
plt.scatter(x2,y2,label='12月份')
# 设置x轴刻度
_x1 = list(x1)+list(x2)
x_label = ['4月{}日'.format(i) for i in x1]
x_label += ['12月{}日'.format(i-15) for i in x2]
plt.xticks(_x1[::2],x_label[::2],fontproperties=my_font,rotation=45)
# 设置单位
plt.xlabel('日期', fontproperties=my_font)
plt.ylabel('温度', fontproperties=my_font)
# 设置表格透明度
plt.grid(alpha=0.4)
# plt.show()
# 添加图例
plt.legend(prop=my_font)
plt.savefig('./img/2.png')

4.柱状图(plt.bar)

"""
柱状图表示班里喜欢各种球类的分布情况
"""

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname='C:WindowsFontssimhei.ttf')

a = ['篮球', '足球', '排球', '乒乓球', '桌球']
b = [23, 34, 12, 23, 34]
_a = range(len(a))
_x = list(range(len(a)))   # [0,1,2,3,4]
plt.bar(_x, b, width=0.3)
plt.xticks(_a, a, fontproperties=my_font)

5.横向柱状图(plt.barh)

"""
柱状图分析上映电影连续三天对对票房情况
"""

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname='C:WindowsFontssimhei.ttf')
plt.figure(figsize=(20, 8), dpi=80)
a = ['猩球崛起', '战狼2', '心花怒放', '十万个问什么']
b1 = [7, 12, 9, 2]
b2 = [6, 9, 10, 3]
b3 = [3, 12, 14, 8]
bar_wight = 0.2
_b1 = list(range(len(a)))
_b2 = [i+bar_wight for i in _b1]
_b3 = [i+bar_wight*2 for i in _b1]
plt.bar(_b1, b1, width=bar_wight, label='10月1日')
plt.bar(_b2, b2, width=bar_wight, label='10月2日')
plt.bar(_b3, b3, width=bar_wight, label='10月3日')
plt.xticks(_b2, a, fontproperties=my_font)
plt.legend(prop=my_font)
plt.show()
原文地址:https://www.cnblogs.com/wm0217/p/11755521.html