使用matplotlib绘制常见图像
**导入的包及设置
**import matplotlib.pyplot as plt
import os
import random
picture_path = os.path.join(os.getcwd(),“picture”)
plt.rcParams[‘font.sans-serif’] = [‘SimHei’] # 设置中文显示
1.散点图
设置图片大小等信息
fig = plt.figure(figsize=(20, 8),dpi=80)
# 准备数据
x = [1,2,3,4,5,6,7]
y = [1, 2, 4, 9, 16, 25, 36] # 数据准备
# 画出图
plt.scatter(x,y)
# 设置相关属性
plt.title("plot_test",fontsize=25)
plt.xlabel("number",fontsize=20)
plt.ylabel("value",fontsize=20)
plt.tick_params(axis='both',labelsize=20)
plt.xticks(range(7)[::2])
# 保存
plt.savefig(os.path.join(picture_path,"test.png"))
plt.show()
-
折线图
设置图片大小等信息
fig = plt.figure(figsize=(20, 8), dpi=80)
准备数据
x = range(120)
y1 = [random.randint(1,10) + i for i in range(120)]
y2 = range(120)设置相关属性
plt.title(“plot_test”, fontsize=25)# 标题
plt.xlabel(“number”, fontsize=20)# 横坐标显示
plt.ylabel(“value”, fontsize=20)# 纵坐标显示x刻度
_xtick10 = [“10点{}分”.format(i) for i in x[0:60]]
_xtick11 = [“11点{}分”.format(i-60) for i in x[60:]]
_xtick = _xtick10 + _xtick11
plt.xticks(x[::5],_xtick[::5])画出图
plt.plot(x,y2,‘r-’,label=“test1”,alpha=0.3,linewidth=1)
plt.plot(x,y1,“g-”,label=“random”,alpha = 0.8,linewidth=1)
plt.legend(loc=“best”,fontsize=50)# 添加图例保存
plt.savefig(os.path.join(picture_path, “test2.png”))
plt.show()
3.竖状条形图
条形图
linewidth = 0.2
# 设置图片大小等信息
fig = plt.figure(figsize=(20, 8), dpi=80)
# 准备数据
x1 = [ i for i in range(12)]
x2 = [i + linewidth for i in range(12)]
x3 = [i + linewidth*2 for i in range(12)]
y1 = [random.randint(1, 6) + i for i in range(12)]
y2 = [random.randint(5, 9) + i for i in range(12)]
y3 = [random.randint(2, 6) + i for i in range(12)]
# 设置相关属性
plt.title("plot_test", fontsize=25) # 标题
plt.xlabel("number", fontsize=20) # 横坐标显示
plt.ylabel("value", fontsize=20) # 纵坐标显示
# x刻度
_xtick1 = ["电影{}".format(i) for i in x1]
plt.xticks(x2, _xtick1)
# 画出图
plt.bar(x1, y1,label="今天",width=linewidth,color="orange",alpha=0.6)
plt.bar(x2,y2, label="明天",width=linewidth, color="blue", alpha=0.6)
plt.bar(x3, y3,label="后天", width=linewidth, color="red", alpha=0.2)
# 添加图例
plt.legend(loc='best')
# 保存
plt.savefig(os.path.join(picture_path, "test3.png"))
plt.show()
4.横状条形图
# 条形图
linewidth = 0.2
# 设置图片大小等信息
fig = plt.figure(figsize=(20, 8), dpi=80)
# 准备数据
x1 = [i for i in range(12)]
x2 = [i+linewidth for i in range(12)]
x3 = [i + linewidth*2 for i in range(12)]
y1 = [random.randint(1, 6) + i for i in range(12)]
y2 = [random.randint(5, 9) + i for i in range(12)]
y3 = [random.randint(2, 6) + i for i in range(12)]
# 设置相关属性
plt.title("plot_test", fontsize=25) # 标题
plt.xlabel("数量", fontsize=20) # 横坐标显示
plt.ylabel("电影", fontsize=20) # 纵坐标显示
# y刻度
_ytick = ["电影{}".format(i) for i in range(12)]
plt.yticks(x2,_ytick)
# 画出图
plt.barh(x1, y1, label="今天", height=linewidth, color="orange", alpha=0.6)
plt.barh(x2, y2, label="明天", height=linewidth, color="blue", alpha=0.6)
plt.barh(x3, y3, label="后天", height=linewidth, color="red", alpha=0.5)
# 添加图例
plt.legend(loc='best')
# 保存
plt.savefig(os.path.join(picture_path, "test4.png"))
plt.show()
5.柱状图
设置图像
fig = plt.figure(figsize=(20, 8), dpi=80)
# 准备数据
a=[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]
group = 11
dist = (max(a) - min(a))//11
plt.xlabel("电影时长")
plt.ylabel("次数")
x的刻度映射
plt.xticks(range(min(a),max(a)+dist,dist))
plt.hist(a, group)
plt.grid(linestyle="-.",alpha=0.5)
plt.savefig(os.path.join(picture_path, "test5.png"))
plt.show()