数据分析_1_matplotlib_1_折线图
第一部分
from matplotlib import pyplot as plt
import random
import matplotlib
# x = range(2, 26, 2)
# y = [25, 12, 45, 23, 45, 24, 12, 32, 43, 12, 12, 23]
#
# fig = plt.figure(figsize=(20, 8), dpi=80)
# plt.plot(x, y)
# # plt.savefig("./sig_size3.png")
# # plt.savefig("./sig_size3.svg")
# plt.xticks(range(1, 26))
# plt.yticks(range(min(y), max(y)+1))
# plt.show()
x = range(120)
y = [random.randint(20, 35) for i in range(120)]
# 生成120个在20-35之间的随机整数
# plt.figure(figsize=(30, 8), dpi=80)
# 中文字符不显示问题
# 方案1
font = {
'family': 'MicroSoft YaHei',
'weight': 'bold',
}
matplotlib.rc('font', **font)
# 查看rc源码获取更多
# 方案2
# from matplotlib import font_manager
# my_font = font_manager.FontProperties(fname='字体路径')
# plt.xticks(_x[::3], _x_tables[::3], rotation=270, fontproperties=my_font)
# 查看rc源码获取更多
_x = list(x)
_x_tables = ["10时{}分".format(i) for i in range(60)]
_x_tables += ["11时{}分".format(i) for i in range(60)]
plt.xticks(_x[::3], _x_tables[::3], rotation=45)
# 传str 一个对应一个
# rotation 旋转x刻度的显示
# 设置图片描述信息
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("气温变化图")
plt.plot(x, y)
plt.show()
第二部分
from matplotlib import pyplot as plt
import random
import matplotlib
font = {
'family': 'MicroSoft YaHei',
'weight': 'bold',
}
matplotlib.rc('font', **font)
y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
y_2 = [random.randint(0, 5) for i in range(20)]
x = range(11, 31)
_x = x
_x_table = ["{}岁".format(i) for i in _x]
plt.xticks(_x, _x_table, rotation=45)
plt.xlabel('年龄')
plt.ylabel('个数')
plt.title('XXX图')
# 绘制网格 稀疏按照xy的刻度
plt.grid(alpha=0.2)
"""
alpha:透明度
"""
# 绘制多张表
"""
color:线条颜色(或十六进制)
linestyle:线条风格
linewidth:线宽
alpha:透明度
label:图例标签
"""
plt.plot(x, y_1, label="小明")
plt.plot(x, y_2, label="小红")
# 将图例信息展示出来
"""
loc:显示位置默认右上
prop:指定字体
"""
plt.legend()
# 添加水印
# 标注最大值最小值
plt.show()

浙公网安备 33010602011771号