赞
踩
对python可视化处理,进行补充说明,matplotlib 进行补充说明,对图例,标题,刻度,坐标轴等进行了更加充分的说明和展示,代码和运行结果图像展示如下:(整理不易,点个赞再走吧,谢谢)
函数 legend(),title()
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- import numpy as np
-
- mpl.rcParams["font.sans-serif"] = ["simhei"]
- mpl.rcParams["axes.unicode_minus"] = False
-
- x = np.linspace(-2*np.pi,2*np.pi,200)
- y = np.sin(x)
- y1 = np.cos(x)
- #r"$$"模式 输出印刷级别的文档效果
- plt.plot(x,y,label = r"$\sin(x)$")
- plt.plot(x,y1,label = r"$\cos(x)$")
-
- plt.legend(loc = "lower left")
- plt.title("正弦函数和余弦函数的折线图")
-
- plt.show()
'运行
函数参数:legend()
loc #位置参数
bbox_to_anchor #线框位置参数
title #图例标签内容的标题参数
shadow #线框阴影
fancybox #线框圆角处理参数
其中 loc 位置
upper right 1
upper left 2
lower left 3
lower right 4
center left 6
center right 7
lower center 8
upper center 9
center 10
- 案例1-图例的展示样式的调整
- '''
- 函数参数:legend()
- loc #位置参数
- bbox_to_anchor #线框位置参数
- title #图例标签内容的标题参数
- shadow #线框阴影
- fancybox #线框圆角处理参数
- loc 位置
- upper right 1
- upper left 2
- lower left 3
- lower right 4
- center left 6
- center right 7
- lower center 8
- upper center 9
- center 10
- '''
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- import numpy as np
-
- x = np.arange(0,2.1,0.1)
- y = np.power(x,3)
- y1 = np.power(x,2)
- y2 = np.power(x,1)
-
- plt.plot(x,y,ls = "-",lw = 2,label = "$x^{3}$")
- plt.plot(x,y1,ls = "-",lw = 2,c = "r",label = "$x^{2}$")
- plt.plot(x,y2,ls = "-",lw = 2,c = "y",label = "$x^{1}$")
-
-
- plt.legend(loc = "upper left",#位置参数
- bbox_to_anchor = (0.05,0.95),#线框位置参数
- ncol = 3,
- title = "power function",#图例标签内容的标题参数
- shadow = True,#线框阴影
- fancybox = True#线框圆角处理参数
- )
-
- plt.show()
#关键字参数 包括:1.字体样式,2.字体大小,3.字体风格4.字体颜色
- 案例2-标题的展示样式的调整
- #关键字参数 包括:1.字体样式,2.字体大小,3.字体风格4.字体颜色
-
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- import numpy as np
-
- x = np.linspace(-2,2,1000)
- y = np.exp(x)
-
-
- plt.plot(x,y,ls = "-",lw = 2,color = "g")
- plt.title("center demo")
-
- plt.title("Left Demo",
- loc = "left",
- fontdict = {"size":"xx-large",
- "color":"r",
- "family":"Times New Roman"}
- )
-
- plt.title("right demo",
- loc = "right",
- family = "Comic Sans MS",#字体类别
- size = 20,#字体大小
- style = "oblique",#字体风格
- color = "c"#字体颜色
- )
-
- plt.show()
- 案例3-带图例的饼图
-
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- import numpy as np
-
-
- mpl.rcParams["font.sans-serif"] = ["simhei"]
- mpl.rcParams["axes.unicode_minus"] = False
-
- elements = ["面粉","砂糖","奶油","草莓酱","坚果"]
- weight = [40,15,20,10,15]
- colors = ["#1b9e77","#d95f02","#7570b3","#66a61e","#e6ab02"]
-
- wedges,texts,autotexts = plt.pie(weight,
- autopct = "%3.1f%%",
- textprops = dict(color = "w"),
- colors = colors
- )
-
-
- plt.legend(wedges,
- elements,
- fontsize = 12,
- title = "配料表",#图例标签内容的标题参数
- loc = "center left",#位置参数
- bbox_to_anchor = (0.91,0,0.3,1),#线框位置参数
- shadow = True,#线框阴影
- fancybox = True#线框圆角处理参数
- )
-
-
- plt.setp(autotexts,size = 15,weight = "bold")
- plt.setp(texts,size = 12)
- plt.title("果酱面包配料比例表")
- plt.show()
函数 xlim(),xtikcs()
- # 函数 xlim(),xtikcs()
-
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- import numpy as np
-
- x = np.linspace(-2*np.pi,2*np.pi,200)
- y = np.sin(x)
-
- plt.subplot(211)
- plt.plot(x,y)
- plt.subplot(212)
- plt.xlim(-2*np.pi,2*np.pi)
-
- plt.xticks([-2*np.pi,-3/2*np.pi,-1*np.pi,-1/2*np.pi,0,1/2*np.pi,1*np.pi,3/2*np.pi,2*np.pi],
- [r"$-2\pi$",r"$-3/2\pi$",r"$-1\pi$",r"$-1/2\pi$",0,r"$1/2\pi$",r"$1\pi$",r"$3/2\pi$",r"$2\pi$"]
- )
-
- plt.plot(x,y)
- plt.show()
'运行
函数 subplot() 子区函数 绘制几何形状相同的网格区域
- 案例-逆序设置坐标轴刻度标签
-
-
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- import numpy as np
-
-
- mpl.rcParams["font.sans-serif"] = ["fangsong"]
- mpl.rcParams["axes.unicode_minus"] = False
-
- time = np.arange(1,11,0.5)
- machinePower = np.power(time,2) + 0.7
-
- plt.plot(time,machinePower,
- linestyle = "-",
- linewidth = 2,
- color = "r")
-
- plt.xlim(10,1)
- plt.xlabel("使用年限")
- plt.ylabel("机器功率")
- plt.title("机器损耗曲线")
- plt.grid(ls = ":",lw = 1,color = "gray",alpha = 0.5)
-
- plt.show()
cellText #表格的数值,将源数据按照行进行分组,每组数据放在列表里存储,所有组数据再放到列表里存储
cellLoc #表格中数据对齐位置,左对齐、居中、右对齐
colWidths #表格每列的宽度
colLabels #表格每列的列名称
colColours #表格每列的列名称所在的单元格的颜色
rowLables #表格每行的行名称
rowLoc #表格每行的行名称对齐位置,可以左对齐、居中、右对齐
loc #表格在画布中的位置
- '''
- 函数参数:table()
- cellText #表格的数值,将源数据按照行进行分组,每组数据放在列表里存储,所有组数据再放到列表里存储
- cellLoc #表格中数据对齐位置,左对齐、居中、右对齐
- colWidths #表格每列的宽度
- colLabels #表格每列的列名称
- colColours #表格每列的列名称所在的单元格的颜色
- rowLables #表格每行的行名称
- rowLoc #表格每行的行名称对齐位置,可以左对齐、居中、右对齐
- loc #表格在画布中的位置
- '''
-
- import matplotlib as mpl
- import matplotlib.pyplot as plt
-
- mpl.rcParams["font.sans-serif"] = ["fangsong"]
- mpl.rcParams["axes.unicode_minus"] = False
-
- labels = "A难度水平","B难度水平","C难度水平","D难度水平"
- students = [0.35,0.15,0.20,0.35]
- explode = (0.1,0.1,0.1,0.1)
- colors = ["#377eb8","#e41a1c","#4daf4a","#984ea3"]
-
-
- plt.pie(students,
- explode = explode,
- labels = labels,
- autopct = "%1.1f%%",
- startangle = 45,
- shadow = True,
- colors = colors)
-
- plt.title("选择不同难度测试试卷的学生占比")
-
- colLabels = ["A难度水平","B难度水平","C难度水平","D难度水平"]
- rowLabels = ["学生选择试卷人数"]
-
- studentValues = [[350,150,200,300]]
-
- colColors = ["#377eb8","#e41a1c","#4daf4a","#984ea3"]
-
- plt.table(cellText = studentValues,
- cellLoc = "center",
- colWidths = [0.1] * 4,
- colLabels = colLabels,
- colColours = colColors,
- rowLabels = rowLabels,
- rowLoc = "center",
- loc = "bottom")
-
- plt.savefig('plot.png', dpi=300)# 保存图像并提高清晰度
- plt.show()
'运行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。